我写了这个方法来读取我的数据库,但每次调用它时,它都会创建一个带有nil NSManaged Objects的新核心数据对象。为什么会这样?
<小时/>
func readDatabaseWithStringArrayAndStrings() -> (String, [MyContactCard]?)
{
let context = self.appDelegate!.persistentContainer.viewContext
let request = MyContactCard.createFetchRequest()
request.returnsObjectsAsFaults = false
do{
var str="" //str obj
let myResults = try context.fetch(request)
print("myResults results: \(myResults)")
return (str, myResults)
}
catch{
return (error.localizedDescription, nil)
}
}
//caller
_ = MyContactCard(context: self.appDelegate.persistentContainer.viewContext).
readDatabaseWithStringArrayAndStrings()
输出