Xcode 9 中的 Swift 4 - 读取核心数据中的数据失败

克罗克斯

我的函数read()有一个不能正常工作的谓词,我设置“no”0可以读取所有数据,如果我设置其他则不能。

class DataClass
{
    private(set) var no: Int16?
    private(set) var title: String?
    private(set) var content: NSAttributedString?

    init(no: Int16,
         title: String,
         content: NSAttributedString)
    {
        self.no = no
        self.title = title
        self.content = content
    }
}


func read(no: Int16) -> DataClass?
    {
        let ReadData = DataClass()

        let appDelegate:AppDelegate = UIApplication.shared.delegate as! AppDelegate
        let managedObjectContext = appDelegate.persistentContainer.viewContext

        let entity = NSEntityDescription.entity(forEntityName: "Note", in: managedObjectContext)

        let request = NSFetchRequest<Note>(entityName: "Note")
        request.fetchOffset = 0
        request.entity = entity

        print("noBtnDn: \(no)")
        let predicate = NSPredicate(format: "no == %@", NSNumber(value: no))
        request.predicate = predicate

        do{
            let results = try managedObjectContext.fetch(request)
            print("results`count: \(results.count)")
            for note in results
            {
                //ReadData.title = note.title!
                //ReadData.content = (note.content as? NSAttributedString)!
                ReadData.ReadFromNote(note: note)
                print("no: \(note.no)")
            }
        }
        catch{
            print("Failed to read data.")
            return nil
        }
        return ReadData

    }

当 no!=0 时,它打印结果count: 24* when no=0 (the count of all data is 24),print *results计数: 0。数据的“否”没有 0,它们从 1 开始。

所以当 no=0 时,打印

noBtnDn: 0
results` count: 24
no: 1
no: 2
no: 3
...
no: 24

我不知道为什么它找不到正确的“否”,是我的函数使用不正确吗?

雷姆

您的课程与 CoreData 属性匹配吗?

您是否也尝试将可选的 Int16 设为非 Optional ?

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

闭包元组不支持Xcode 9 Swift 4中的解构

Xcode 4核心数据:如何使用在数据模型编辑器中创建的访存属性

Swift无法在Xcode测试中测试核心数据吗?

Swift:XCode6 Beta 5在AppDelegate中的核心数据对象上给出了错误

使用Swift和XCode将整数存储在核心数据中

如何使用Swift 3 XCode 8在核心数据中预加载数据库

Xcode 9卡在Swift 4迁移上

在Swift 4 Xcode 9 Beta 6中无法对UIButton进行编程,无法添加目标

XCode 9 Swift 4中没有这样的模块'FBAudienceNetwork'

更新到Xcode 9和Swift 4

MFMailComposeViewController,Swift 4,Xcode 9

在Swift 4 Xcode 9中比较Int数组切片元素时出错

Xcode 9中的Swift 4-如何在NSAttributedstring中使用boundingrect?

在核心数据中,如何在swift 4中执行“如果存在,则将更新插入”?

Swift4-Xcode 9 PCH错误

setValuesForKeys在Xcode 9中的Swift 4中不起作用

我是否需要在Xcode 9,Swift 4中使用cocoapods明确声明要安装哪个版本的alamofire

iOS Gmail API-用于在Swift 4 / Xcode9中检索给定标签的Gmail邮件的示例代码

Swift核心数据:核心数据中的枚举

Xcode核心数据显示UILabel中的所有实体

Swift核心数据问题(Xcode 6)

核心数据中的Swift元组

XCode 7 Beta / Swift 2中的单元测试核心数据

Xcode 8中的核心数据,关于创建NSManagedObject

无法将字符串数组从核心数据转换为 Int 数组并在 Swift 4 中求和

UnrecognizedSelector 如何在 Xcode9/Swift4 中成为可能?

使用 Swift 4 和 Xcode 9 获取 JSON 数据

可选类型“日期?”的值 未解开;你的意思是使用“!” 或者 '?'?在 Swift 4 XCode 9 中

Xcode 中的 Swift 核心数据使用