使用predicateWithFormat,%@被“”包围。我们需要为键使用%K。为什么%@在predicateWithFormat和stringWithFormat之间有不同的表现?
例如[NSPredicate predicateWithFormat @"%@ == %@" ,@"someKey",@"someValue"]
成为
"someKey" == "someValue"
虽然在stringWithFormat,%@不被包围 “”
[NSString stringWithFormat @"%@ == %@" ,@"someKey",@"someValue"]
成为someKey == someValue
为什么不同?
我错过了什么吗?
为什么在predicateWithFormat中使用%@作为“Value”,因为它不是stringWithFormat中的%@意思。为什么不创建一个新的表示法,例如%V来获取“Value”和%@仍然像stringWithFormat对应值那样。
为什么苹果决定相同的符号,即%@应该有不同的含义。
他们真的不一样吧?我错过了什么?
在我问这个问题之前,我完全理解了这一点。但是为什么在谓词中使用%@作为“Value”。为什么不创建一个新的表示法,例如%V来获取“Value”和%@仍然像stringWithFormat对应值那样。 –
嗯,这不完全正确'%@'只会在字符串对象上添加引号,例如,如果您将它与'NSNumber'(例如'age ==%@')一起使用,它将**不引用该值(它会扩展到'age == 10')。 – Alladinian
我知道,但即使对于字符串对象,%@也不会为stringWithFormat添加引号。所以基本上,%@对于字符串对象的predicateWithFormat行为异常。不寻常是不好的。 –