NSPredicate – 无法解析格式字符串
我正在尝试使用此字符串“ 193e00a75148b4006a451452c618ccec
”编写NSPredicate
以获取具有my_column值的行,并且我得到以下崩溃。
因未捕获的exception’NSInvalidArgumentException’而终止应用程序,原因:’无法解析格式字符串“my_column = 193e00a75148b4006a451452c618ccec”’
我的谓词陈述
fetchRequest.predicate=[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@==\"%@\"",attributeName,itemValue]];
也尝试了这个
fetchRequest.predicate=[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@",attributeName,itemValue]];
这个
fetchRequest.predicate=[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ = %@",attributeName,itemValue]];
和这个
fetchRequest.predicate=[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@=\"%@\"",attributeName,itemValue]];
请帮忙。
当我尝试使用Martin R的答案时,我发现了这一点
fetchRequest.predicate=[NSPredicate predicateWithFormat:@"%@==%@",attributeName,itemValue];
attributeName我传递带有”所以我取下了attributeName并硬编码了它,然后它工作正常。
将字符串括在单引号中:
[NSPredicate predicateWithFormat:@"my_column = '193e00a75148b4006a451452c618ccec'"]
或者更好 ,使用参数替换:
[NSPredicate predicateWithFormat:@"my_column = %@", @"193e00a75148b4006a451452c618ccec"]
如果搜索字符串包含诸如'
或"
类的特殊字符,则第二种方法可以避免问题。
备注:在构建谓词时,切勿使用stringWithFormat
。 stringWithFormat
和predicateWithFormat
处理%K
和%@
格式,因此组合这两种方法非常容易出错(并且不必要)。
我认为你在检查平等时错过了”
现在试试,
[NSPredicate predicateWithFormat:@"my_column='193e00a75148b4006a451452c618ccec'"];
尝试
//Case Insensitive fetchRequest.predicate=[NSPredicate predicateWithFormat:@"%K = [cd] %@",attributeName,itemValue]; //Case sensitive fetchRequest.predicate=[NSPredicate predicateWithFormat:@"%K = %@",attributeName,itemValue];
- 如何使一个string上的超链接和在Facebook分享
- 什么原因导致CBCentralManagerStateUnknown在iOS?
- Captive Wifi Popup:点击一个链接打开Safari
- 如何在iOS中使用xmpp阻止朋友?
- 转换iOS PT到Android DP?
- iOS – 与通用链接的错误
- iOS 7:在UIActionSheet委托函数中创build的UIAlertView不能自动旋转
- UIImagePickerController:未调用didFinishPickingMediaWithInfo
- Swift / iOS:如何在AnyObject / Any或Pointers的函数中使用inout参数