我有代码,它提取核心数据
var appDelegate : AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
var managedContext : NSManagedObjectContext = appDelegate.managedObjectContext!
let request = NSFetchRequest(entityName: "QuestionDB")
request.returnsObjectsAsFaults = false
if let results = managedContext.executeFetchRequest(request, error: nil)
返回AnyObject
我需要它返回类型为[Question]
当我施展
var appDelegate : AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
var managedContext : NSManagedObjectContext = appDelegate.managedObjectContext!
let request = NSFetchRequest(entityName: "QuestionDB")
request.returnsObjectsAsFaults = false
if let results = managedContext.executeFetchRequest(request, error: nil) as? [Question] {
println("This is result of loadFromDb \(results)")
}
它返回nil
我该如何解决?
答案 0 :(得分:1)
答案 1 :(得分:0)
试试这个:
let fetchRequest = NSFetchRequest(entityName:"QuestionDB")
var error: NSError?
let fetchedResults = managedObjectContext.executeFetchRequest(fetchRequest, error: &error) as! [NSManagedObject]?
if let results = fetchedResults {
return results as! [QuestionDB]
} else {
fatalError("Could not fetch \(error), \(error!.userInfo)")
}
如果您的班级名称为Question
,则必须在模型检查员中进行设置。