为什么将此谓词格式转换为'= nil' [英] Why is this predicate format being turned into '= nil'

查看:78
本文介绍了为什么将此谓词格式转换为'= nil'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人建议此线程与我的问题完全相同但是,我的应用程序并没有崩溃,也没有迁移到Swift3。它只是没有返回任何结果。因此,解决方案本质上是相同的,但是我的问题所基于的行为却大不相同。

It was suggested that this thread is an exact duplicate of my question however, my app wasn't crashing and I'm not migrating to Swift 3. It just wasn't returning any results. So the solution is essentially the same but the behavior on which my question is based is quite different.

今天早晨阅读了很多线程后,我很确定此代码是正确的并且应该可以正常工作:

After reading many threads this morning I'm pretty confident this code is correct and should work:

func fetchUnits(weightUnitUid: Int? = nil) -> [WeightUnit] {
    let fetchRequest = NSFetchRequest(entityName: "WeightUnit")
    if let weightUnitFilter = weightUnitUid {
        let filterPredicate = NSPredicate(format: "uid = %@", weightUnitFilter)
        fetchRequest.predicate = filterPredicate
    }

但这是下面的样子调试控制台:

but here's what it looks like in the debugging console:


(lldb)po filterPredicate // uid == nil

(lldb) po filterPredicate // uid == nil

我传递的是0,而 weightUnitUid 实际上是0,所以我希望:

I'm passing in 0 and weightUnitUid is in fact 0 at this point so I'd expect:


uid == 0

uid == 0

我知道 %@ 应该能够处理NSNumber,这就是我所需要的。

I know %@ should be able to handle an NSNumber and that's what I need. Where I'm going wrong.

谢谢

推荐答案

code>%@ 格式需要Foundation对象作为参数,零
解释为 nil

您可以将整数转换为 NSNumber

let filterPredicate = NSPredicate(format: "uid = %@", weightUnitFilter as NSNumber)

或改用 long int格式:

or use the "long int" format instead:

let filterPredicate = NSPredicate(format: "uid = %ld", weightUnitFilter)

这篇关于为什么将此谓词格式转换为'= nil'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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