我正在尝试获取核心数据实体并遇到问题,我认为这是由于我使用了子类NSManagedObject。
尝试打印出其中一个已获取的对象时出现以下错误:
Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)
这是我的“显示”NSManagedObject子类:
import Foundation
import CoreData
@objc(Show)
class Show: NSManagedObject {
@NSManaged var dateUpdated: NSDate
@NSManaged var fontFilename: String
@NSManaged var fontName: String
@NSManaged var idShow: NSNumber
@NSManaged var isActive: NSNumber
@NSManaged var productIdentifier: String
@NSManaged var title: String
}
这是我在助手类CoreDataManager.swift中的提取调用:
func fetchShows() -> Array<AnyObject>?
{
// This function will fetch data for all available shows.
// Return value will be an array of dictionaries (i.e. [0]->{Show, Characters})
// i.e. Show Title, Store Product Identifier, Font, Characters, etc.
var results = Array<AnyObject>()
let context:NSManagedObjectContext = self.managedObjectContext!
var error: NSError?
var fetchRequest = NSFetchRequest()
let entity = NSEntityDescription.entityForName("Show", inManagedObjectContext: context)
fetchRequest.entity = entity
let fetchedObjects = context.executeFetchRequest(fetchRequest, error: &error)! as Array
for item in fetchedObjects
{
var showData = Dictionary<String, AnyObject>()
let show: Show = item as Show
showData.updateValue(show, forKey: "show")
// Get Characters
showData.updateValue(self.fetchCharactersForShow(show.idShow)!, forKey: "characters")
results.append(showData)
// Test
self.printShow(show.idShow) //NOTE - THIS METHOD CREATES A NEW FETCH REQUEST USING THE PROVIDED SHOW ID AND SUCCESSFULLY PRINTS OUT THE SHOW DATA
}
return results
}
以下是我现在尝试从RootViewController.swift访问获取的值的地方:
//TESTING
self.shows = CoreDataManager().fetchShows()!
let showData = self.shows[0] as Dictionary<String, AnyObject>
let show = showData["show"] as Show
println("TEST: Show \(show.title) is active: \(show.isActive.boolValue)")
/******/
以下是“显示”对象的调试器屏幕截图:
答案 0 :(得分:0)
let DataManager = CoreDataManager()
self.shows = DataManager.fetchShows()
现在它有效!