Swift5 简单核心数据 NSPredicate 获取
Swift5 Simple Core Data NSPredicate Get
我只是想通过我命名为 "id" 的字符串类型的 属性 访问核心数据中的记录。下面一直抱怨'Unable to parse the format string "id == 8DF3F2C6741B47C8864D1052C36E2C4D"'。我该如何解决这个问题?
private func getEntity(id: String) -> NSManagedObject? {
var myEntity: NSManagedObject?
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "MyEntity")
fetchRequest.predicate = NSPredicate(format: "id = \(id)")
do {
var tempArray = try getCurrentManagedContext().fetch(fetchRequest)
myEntity = tempArray.count > 0 ? tempArray[0] : nil
} catch let error as NSError {
print("get failed ... \(error) ... \(error.userInfo)")
}
return myEntity
}
这是一个格式字符串。而不是
NSPredicate(format: "id = \(id)")
写
NSPredicate(format: "id == %@", id)
我只是想通过我命名为 "id" 的字符串类型的 属性 访问核心数据中的记录。下面一直抱怨'Unable to parse the format string "id == 8DF3F2C6741B47C8864D1052C36E2C4D"'。我该如何解决这个问题?
private func getEntity(id: String) -> NSManagedObject? {
var myEntity: NSManagedObject?
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "MyEntity")
fetchRequest.predicate = NSPredicate(format: "id = \(id)")
do {
var tempArray = try getCurrentManagedContext().fetch(fetchRequest)
myEntity = tempArray.count > 0 ? tempArray[0] : nil
} catch let error as NSError {
print("get failed ... \(error) ... \(error.userInfo)")
}
return myEntity
}
这是一个格式字符串。而不是
NSPredicate(format: "id = \(id)")
写
NSPredicate(format: "id == %@", id)