我在 Appdelegate 类中编写了两个单独的函数 fetchRecords 和 displayRecords 。 fetchRecords 函数将从实体中获取所有记录,并且工作正常。displayRecords 函数接受来自 fetchRecords 函数的返回值并打印所有记录逐个。
我有一个 View Controller ,它调用这两个函数来完成所需的任务。我的问题是 displayRecords 中的 result.count 显示获取的记录中可用的记录总数。在打印记录时,值是零。
override func viewDidLoad() {
super.viewDidLoad()
let fetchedRecords = AppDelegate().fetchRecords(fromEntity: "Dashboard")
AppDelegate().displayRecords(fromFetchedRecords: fetchedRecords)
}
这里是在 AppDelegate 类中编写的 fetchRecords 和 displayRecords 函数
func fetchRecords(fromEntity entity:String) -> Array<AnyObject> {
var fetchedResult:Array<AnyObject> = []
let fetchRequest = NSFetchRequest()
let entityDescription = NSEntityDescription.entityForName(entity, inManagedObjectContext: self.managedObjectContext)!
fetchRequest.entity = entityDescription
do{
fetchedResult = try self.managedObjectContext.executeFetchRequest(fetchRequest)
}catch{
let fetchError = error as NSError?
print(fetchError!)
}
return fetchedResult
}
func displayRecords(fromFetchedRecords result:Array<AnyObject>) {
print("Total records:\(result.count)")
if (result.count > 0) {
for data in result {
let dashboard = data as! NSManagedObject
print("Value: \(dashboard.valueForKey("count"))")
}
}
}
在此处添加我的数据模型
我也会分享数据插入代码。
func saveDashBoardData(dictionary: Dictionary<String, String>) {
print(NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask))
//Create Manage Object
let entityDescription: NSEntityDescription = NSEntityDescription.entityForName("Dashboard", inManagedObjectContext: self.managedObjectContext)!
for data in dictionary {
let dashboardObject = Dashboard(entity: entityDescription,insertIntoManagedObjectContext: self.managedObjectContext)
dashboardObject.type = data.0
dashboardObject.count = data.1
do {
try self.managedObjectContext.save()
print("Data saved succesfully")
}
catch {
print(error)
}
}
}
Best Answer-推荐答案 strong>
问题是 AppDelegate() 总是创建一个与应用程序的“硬编码”委托(delegate)实例不同的类的新实例。
你必须写
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let fetchedRecords = appDelegate.fetchRecords(fromEntity: "Dashboard")
appDelegate.displayRecords(fromFetchedRecords: fetchedRecords)
由于您创建了 NSManagedObject 的自定义子类,因此将其用作类型而不是 NSManagedObject 或更糟糕的是 - 未指定的 AnyObject 。它使许多事情变得更容易:
func fetchRecords(fromEntity entity:String) -> [Dashboard] {
let fetchRequest = NSFetchRequest()
let entityDescription = NSEntityDescription.entityForName(entity, inManagedObjectContext: self.managedObjectContext)!
fetchRequest.entity = entityDescription
do {
return try self.managedObjectContext.executeFetchRequest(fetchRequest) as! [Dashboard]
} catch let fetchError as NSError {
print(fetchError!)
}
return [Dashboard]()
}
func displayRecords(fromFetchedRecords result:[Dashboard]) {
print("Total records:\(result.count)")
for dashboard in result { // the check for empty is not needed
print("Value: \(dashboard.count)")
}
}
关于ios - 核心数据 - NSManagedObject 返回 nil,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/36935505/
|