NSPredicate execureFetchRequest不起作用-可能由于字符串中的空格 [英] NSPredicate execureFetchRequest not working - possibly due to spaces in string

查看:48
本文介绍了NSPredicate execureFetchRequest不起作用-可能由于字符串中的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在CoreData中搜索与recordId和字符串名称均匹配的对象,但它并不总是找到该对象。
例如,我正在搜索一个ID为1000且名称为 The Brown Family的对象(注意 The和 Brown之间的2个空格)。
如果我使用:

I am trying to search in CoreData for an object that matches both a recordId and a string name, but it doesn't always find the object. For example, I an searching for an object with id 1000 and name "The Brown Family" (note the 2 spaces between "The" and "Brown"). If I use:

NSPredicate *pred = [NSPredicate predicateWithFormat:@"(recordId == %@") AND (name like[cd] %@)", recordId, name];

,具有recordId = 1000和name = The Brown Family,获取请求返回nil。
如果我使用:

with recordId=1000 and name="The Brown Family", the fetch request returns nil. If I use:

NSPredicate *pred = [NSPredicate predicateWithFormat:@"(recordId == %@"),   recordId];

with recordId = 1000,它将找到对象。如果我打印对象的name属性,则会得到 The Brown Family。
因此,该对象具有正确的ID和名称,但我的fetchRequest失败了。我做错了吗?

with recordId=1000, it finds the object. If I print the object's name property, I get "The Brown Family". So the object is there with the correct id and name, but my fetchRequest fails. What am I doing wrong?

推荐答案

您可能需要将值用单引号引起来...

You might need to enclose the value in single quotes...

NSPredicate *pred = [NSPredicate predicateWithFormat:@"(recordId == %@) AND (name like[cd] '%@')", recordId, name];

这篇关于NSPredicate execureFetchRequest不起作用-可能由于字符串中的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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