gpt4 book ai didi

ios - 核心数据 - NSManagedObject 返回 nil

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:56:58 24 4
gpt4 key购买 nike

我在 Appdelegate 类中编写了两个单独的函数 fetchRecordsdisplayRecordsfetchRecords 函数将从实体中获取所有记录并且它工作正常。displayRecords 函数接受来自 fetchRecords 函数的返回值并打印所有记录逐个。

我有一个 View Controller 调用这两个函数来完成所需的任务。我的问题是 displayRecords 中的 result.count 显示获取的记录中可用的记录总数。当一条一条地打印记录时,该值为 nil。

override func viewDidLoad() {
super.viewDidLoad()
let fetchedRecords = AppDelegate().fetchRecords(fromEntity: "Dashboard")
AppDelegate().displayRecords(fromFetchedRecords: fetchedRecords)
}

这里是 AppDelegate 类中编写的 fetchRecordsdisplayRecords 函数

  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"))")
}
}
}

在这里添加我的数据模型 enter image description here

我也会分享数据插入代码。

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)
}
}
}

最佳答案

问题是 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com