gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 11:38:29 25 4
gpt4 key购买 nike

我的函数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 时(所有数据的计数为 24),打印 resultscount: 24*,当 no!=0 时,打印 *results count: 0。数据的“号”没有0,它们从1开始。

所以当 no=0 时,打印

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

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

最佳答案

您的类与核心数据属性匹配吗?

您是否还尝试将可选的 Int16 变为非可选?

关于ios - Xcode 9 中的 Swift 4 - 读取核心数据中的数据失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49474104/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com