case insensitive NSPredicate with single result in CoreData

隐身守侯 提交于 2021-01-16 12:29:35

问题


Here is my current NSPredicate:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"UPC==%@ OR ItemID==%@", aUPCCode,aUPCCode];

How can I make this case insensitive?

And I do not want to do any partial matching.

Example if they enter 123 for aUPCCode I do not want to get 123, 123a, 123b, 123c, ect. I would only want an exact match.

I thought about doing this but it seems a little ridiculous:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"UPC==%@ OR ItemID==%@ OR UPC==%@ OR ItemID==%@ OR UPC==%@ OR ItemID==%@", aUPCCode,aUPCCode,[ aUPCCode lowercaseString] ,[aUPCCode lowercaseString], [aUPCCode uppercaseString],[aUPCCode uppercaseString]];

回答1:


As Dave DeLong said, you can use:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"UPC ==[c] %@ OR ItemID ==[c] %@", aUPCCode,aUPCCode];

Edit:

Use ==[c] instead of ==[cd] or you get accents too (abcd == àbcd).




回答2:


As Sinetris said, the above works in Objective-C.

It works for case-insensitive. Fetches all results which have the "example" string value in it.

Update Swift 4.0/5.0

let predicateIsNumber = NSPredicate(format: "keywordContactNo contains[c] %@", example!) 

Hope it helps

Thanks




回答3:


maybe this:

[NSPredicate predicateWithFormat:@"UPC MATCHES[cd] %@ OR ItemID MATCHES[cd] %@",aUPCCode,aUPCCode];


来源:https://stackoverflow.com/questions/7546496/case-insensitive-nspredicate-with-single-result-in-coredata

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!