【发布时间】:2015-04-21 15:34:17
【问题描述】:
我试图在 Swift 中使用 NSPredicate 来查询 Core Data,但是在尝试运行它时会抛出 EXC_BAD_ACCESS(Code=1, address=0x1) 错误,我做错了什么?
这是发生错误的文件
class LevelsScreenModel : UIViewController {
func getWord(level: Int, section: Int) -> String
{
let fetchRequest = NSFetchRequest(entityName: "Words")
//This is the line where the error happens
fetchRequest.predicate = NSPredicate(format: "level = %@", level)
fetchRequest.predicate = NSPredicate(format: "section = %@", section)
let word = AppDelegate().managedObjectContext!.executeFetchRequest(fetchRequest, error: nil) as [Words]
if(word.count > 1)
{
for words in word
{
println(words.word)
return words.word
}
}
return "ERROR"
}
}
【问题讨论】:
-
最终错误是由于无法执行“AppDelegate()”。访问 managedObjectContext。相反,这段代码对我有用“var appDel:AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)”希望这对任何人都有帮助
标签: swift core-data nspredicate