开发者

NSFetchRequest fires NSInvalidArgumentException when query ontains apostrophe

开发者 https://www.devze.com 2023-04-05 14:39 出处:网络
When I create query, that contains apostrophe, I got error: \'NSInvalidArgumentException\', reason: \'Unable to parse the fo开发者_开发技巧rmat

When I create query, that contains apostrophe, I got error:

'NSInvalidArgumentException', reason: 'Unable to parse the fo开发者_开发技巧rmat string "SELF.sale.ID == 0 AND ((SELF.dam contains[cd] 'Michael's') OR (SELF.name contains[cd] 'Michael's') OR (SELF.purchaserLocation contains[cd] 'Michael's') OR (SELF.purchaserName contains[cd] 'Michael's') OR (SELF.sire contains[cd] 'Michael's') OR (SELF.vendor contains[cd] 'Michael's'))"'

I have tried to replace single apostrophe with two apostrpohes but still got same error. When the query does not contain apostrophe, everything is going fine. Thanks


My bad, I have to replace single apostrophe with escape string \'


Actually here is the real solution : Issue escaping single quote in iOS with stringByReplacingOccurrencesOfString:widthString

You need to put 2 \, because \ is a character, and therefore needs to be escape to.


This can happen if you surround the format placeholders in your format string with single quotes when using [NSPredicate predicateWithFormat]. These quotes aren't needed.

WRONG Example:

NSPredicate *myPred = [NSPredicate predicateWithFormat:@"name = '%@'";

CORRECT Example:

NSPredicate *myPred = [NSPredicate predicateWithFormat:@"name = %@";
0

精彩评论

暂无评论...
验证码 换一张
取 消