我有一個字串 UUID 進入此方法,用于在 CoreData 中查找將 UUID 保存為 UUID 型別(非字串)的物體。
對于謂詞,我不斷收到“致命錯誤:在展開可選值時意外發現 nil”。
func loadUser(uuid: String) -> [ExistingUsers2] {
let request : NSFetchRequest<ExistingUsers2> = ExistingUsers2.fetchRequest()
let uuidQuery = NSUUID(uuidString: uuid)
request.predicate = NSPredicate(format: "%K == %@", #keyPath(ExistingUsers2.uuid), uuidQuery! as CVarArg)
request.sortDescriptors = [NSSortDescriptor(key: "name", ascending: true)]
do {
existingUsersArray = try context.fetch(request)
print("Returned \(existingUsersArray.count)")
} catch {
print("Error fetching data from context \(error)")
}
return existingUsersArray
}
有什么幫助嗎?我沒有在這里找到任何東西或 Google 博士。TKS
uj5u.com熱心網友回復:
試試這個作為你的謂詞:NSPredicate(format: "cid = %@", "\(id)")
你從字串中得到cid的UUIDinCoreData和idthe在哪里。UUID也不要使用NSUUID。
uj5u.com熱心網友回復:
你可以用這個替換你的謂詞:
guard let uuidQuery = UUID(uuidString: uuid) else { return [] } // no valid UUID with this code
request.predicate = NSPredicate(format: "%K == %@", #keyPath(ExistingUsers2.uuid), uuidQuery as CVarArg)
其他一切都應該作業。
uj5u.com熱心網友回復:
將 替換為uuidAttributeName您的屬性名稱和yourStringuuid您要轉換為 UUID 型別的字串。
var uuid = UUID(uuidString: yourStringuuid)
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "ExistingUsers2")
fetchRequest.predicate = NSPredicate(format: "uuidAttributeName == %@",uuid)
let results = try context.fetch(fetchRequest)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/483965.html
標籤:迅速 核心数据 uuid nspredicate
