Swift NSPredicate 在复合语句时抛出 EXC_BAD_ACCESS(Code=1, address=0x1)
Swift NSPredicate throwing EXC_BAD_ACCESS(Code=1, address=0x1) when compounding statements
我正在尝试在 Swift 中使用 NSPredicate 来查询核心数据,但在尝试 运行 时会抛出 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"
}
}
谓词格式字符串中的 %@
占位符用于 Objective-C
对象,所以你必须将整数包装成 NSNumber
:
fetchRequest.predicate = NSPredicate(format: "level = %@", NSNumber(integer: level))
或使用 ld
来格式化一个(长)整数:
fetchRequest.predicate = NSPredicate(format: "level = %ld", level)
另请注意
fetchRequest.predicate = NSPredicate(format: ...)
fetchRequest.predicate = NSPredicate(format: ...)
不创建复合谓词,简单的秒赋值
覆盖第一个。您可以使用 NSCompoundPredicate
:
let p1 = NSPredicate(format: "level = %ld", level)!
let p2 = NSPredicate(format: "section = %ld", section)!
fetchRequest.predicate = NSCompoundPredicate.andPredicateWithSubpredicates([p1, p2])
或者简单地将谓词与 "AND":
结合起来
fetchRequest.predicate = NSPredicate(format: "level = %ld AND section = %ld", level, section)
您可以使用 PredicatePal 框架,而不是忙于格式转换和 AND
子谓词:
fetchRequest.predicate = *(Key("level") == level && Key("section") == section)
请注意,您需要使用 ==
而不是 =
进行相等比较。
我正在尝试在 Swift 中使用 NSPredicate 来查询核心数据,但在尝试 运行 时会抛出 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"
}
}
谓词格式字符串中的 %@
占位符用于 Objective-C
对象,所以你必须将整数包装成 NSNumber
:
fetchRequest.predicate = NSPredicate(format: "level = %@", NSNumber(integer: level))
或使用 ld
来格式化一个(长)整数:
fetchRequest.predicate = NSPredicate(format: "level = %ld", level)
另请注意
fetchRequest.predicate = NSPredicate(format: ...)
fetchRequest.predicate = NSPredicate(format: ...)
不创建复合谓词,简单的秒赋值
覆盖第一个。您可以使用 NSCompoundPredicate
:
let p1 = NSPredicate(format: "level = %ld", level)!
let p2 = NSPredicate(format: "section = %ld", section)!
fetchRequest.predicate = NSCompoundPredicate.andPredicateWithSubpredicates([p1, p2])
或者简单地将谓词与 "AND":
结合起来fetchRequest.predicate = NSPredicate(format: "level = %ld AND section = %ld", level, section)
您可以使用 PredicatePal 框架,而不是忙于格式转换和 AND
子谓词:
fetchRequest.predicate = *(Key("level") == level && Key("section") == section)
请注意,您需要使用 ==
而不是 =
进行相等比较。