我的函数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
}
当没有!= 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
我不知道为什么它找不到正确的“不”,我的功能使用不正确吗?
答案 0 :(得分:0)
您的类是否与CoreData属性匹配?
您是否也尝试将可选的Int16设为非可选项?