Swift NSPredicate在复合语句时抛出EXC_BAD_ACCESS(Code = 1,address = 0x1) [英] Swift NSPredicate throwing EXC_BAD_ACCESS(Code=1, address=0x1) when compounding statements

查看:184
本文介绍了Swift NSPredicate在复合语句时抛出EXC_BAD_ACCESS(Code = 1,address = 0x1)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Swift中使用NSPredicate来查询核心数据,但它尝试运行它时抛出一个EXC_BAD_ACCESS(Code = 1,address = 0x1)错误,我做错了什么?



这里是错误发生的文件

  class LevelsScreenModel:UIViewController {

func getWord(level:Int,section:Int) - > String
{
let fetchRequest = NSFetchRequest(entityName:Words)

//这是发生错误的行
fetchRequest.predicate = NSPredicate :level =%@,level)
fetchRequest.predicate = NSPredicate(format:section =%@,section)

let word = AppDelegate()。managedObjectContext!.executeFetchRequest (fetchRequest,error:nil)as [Words]

if(word.count> 1)
{
for word in
{
println(words.word)
return words.word
}
}

returnERROR
}
}

解决方案

%@ 谓词格式字符串中的占位符用于Objective-C
对象,因此您必须将整数包装到 NSNumber

  fetchRequest.predicate = NSPredicate(format:level =%@,NSNumber(integer:level))
pre>

或使用 ld 代替格式化一个(长整数):

  fetchRequest.predicate = NSPredicate(format:level =%ld,level)

注意

  fetchRequest.predicate = NSPredicate(format:...) 
fetchRequest.predicate = NSPredicate(format:...)

创建复合谓词,秒分配只需
覆盖第一个。您可以使用 NSCompoundPredicate

  let p1 = NSPredicate 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)


I am trying to use NSPredicate in Swift to query Core Data but it throws an EXC_BAD_ACCESS(Code=1, address=0x1) error when trying to run it, what am I doing wrong?

Here is the file where the error happens

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"
  }
}

解决方案

The %@ placeholder in predicate format strings is for Objective-C objects, so you have to wrap the integer into an NSNumber:

fetchRequest.predicate = NSPredicate(format: "level = %@", NSNumber(integer: level))

or use ld instead to format a (long) integer:

fetchRequest.predicate = NSPredicate(format: "level = %ld", level)

Note also that

fetchRequest.predicate = NSPredicate(format: ...)
fetchRequest.predicate = NSPredicate(format: ...)

does not create a compound predicate, the seconds assignment simply overwrites the first. You can use an NSCompoundPredicate:

let p1 = NSPredicate(format: "level = %ld", level)!
let p2 = NSPredicate(format: "section = %ld", section)!
fetchRequest.predicate = NSCompoundPredicate.andPredicateWithSubpredicates([p1, p2])

or simply combine the predicates with "AND":

fetchRequest.predicate = NSPredicate(format: "level = %ld AND section = %ld", level, section)

这篇关于Swift NSPredicate在复合语句时抛出EXC_BAD_ACCESS(Code = 1,address = 0x1)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆