ios - 核心数据 - NSManagedObject 返回 nil
<p><p>我在 <code>Appdelegate</code> 类中编写了两个单独的函数 <code>fetchRecords</code> 和 <code>displayRecords</code>。 <code>fetchRecords</code> 函数将从实体中获取所有记录,并且工作正常。<code>displayRecords</code> 函数接受来自 <code>fetchRecords</code> 函数的返回值并打印所有记录逐个。 <br/><br/>
我有一个 ViewController ,它调用这两个函数来完成所需的任务。我的问题是 <code>displayRecords</code> 中的 <code>result.count</code> 显示获取的记录中可用的记录总数。在打印记录时,值是零。 </p>
<pre><code>override func viewDidLoad() {
super.viewDidLoad()
let fetchedRecords = AppDelegate().fetchRecords(fromEntity: "Dashboard")
AppDelegate().displayRecords(fromFetchedRecords: fetchedRecords)
}
</code></pre>
<p>这里是在 AppDelegate 类中编写的 <code>fetchRecords</code> 和 <code>displayRecords</code> 函数</p>
<pre><code>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"))")
}
}
}
</code></pre>
<p>在此处添加我的数据模型
<a href="/image/R3gqQ.png" rel="noreferrer noopener nofollow"><img src="/image/R3gqQ.png" alt="enter image description here"/></a> </p>
<p>我也会分享数据插入代码。</p>
<pre><code>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)
}
}
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>问题是 <code>AppDelegate()</code> 总是创建一个与应用程序的“硬编码”委托(delegate)实例不同的类的新实例。</p>
<p>你必须写</p>
<pre><code>let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let fetchedRecords = appDelegate.fetchRecords(fromEntity: "Dashboard")
appDelegate.displayRecords(fromFetchedRecords: fetchedRecords)
</code></pre>
<p>由于您创建了 <code>NSManagedObject</code> 的自定义子类,因此将其用作类型而不是 <code>NSManagedObject</code> 或更糟糕的是 - 未指定的 <code>AnyObject</code>。它使许多事情变得更容易:</p>
<pre><code>func fetchRecords(fromEntity entity:String) -> {
let fetchRequest = NSFetchRequest()
let entityDescription = NSEntityDescription.entityForName(entity, inManagedObjectContext: self.managedObjectContext)!
fetchRequest.entity = entityDescription
do {
return try self.managedObjectContext.executeFetchRequest(fetchRequest) as!
} catch let fetchError as NSError {
print(fetchError!)
}
return ()
}
func displayRecords(fromFetchedRecords result:) {
print("Total records:\(result.count)")
for dashboard in result { // the check for empty is not needed
print("Value: \(dashboard.count)")
}
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 核心数据 - NSManagedObject 返回 nil,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/36935505/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/36935505/
</a>
</p>
页:
[1]