【发布时间】:2017-09-14 02:38:04
【问题描述】:
我想检查我的 NSString 'name' 是否包含“Brittany”或“Bob”,但下面的代码似乎不能解决问题?
ViewController.m
NSPredicate *p = [NSPredicate predicateWithFormat:@"name contains[cd] %@ OR name contains[cd] %@", @"Brittany", @"Bob"];
NSArray *filtered = [self.messages filteredArrayUsingPredicate:p];
知道这应该是什么样子吗?我似乎无法正确使用语法。问题是“过滤”没有返回字符串名称包含“Bob”的数组,只是返回名称包含“Brittany”的数组。
self.messages 包含以下内容:
This is the messages data (
{
body = "Hi";
endswaptime = "<null>";
"first name" = Brittany;
name = Brittany;
nid = 1803;
"node_title" = "Re:";
},
{
body = "It is brittany";
endswaptime = "<null>";
"first name" = Brittany;
name = Brittany;
nid = 1804;
"node_title" = "Re:";
},
{
body = "Hellooo :)\n";
endswaptime = "<null>";
"first name" = Bob;
name = "Bob";
nid = 1805;
"node_title" = "Re:";
}
)
【问题讨论】:
-
您是否尝试过将单个谓词拆分为两个单独的谓词并与 NSCompoundPredicate 结合?
-
我没有 - 你能告诉我那会是什么样子吗?感谢您的帮助!
-
您发布的代码有什么问题?它看起来是正确的。当然,很大程度上取决于
self.messages中实际包含的数据类型。使用有关其内容的详细信息更新您的问题。 -
@rmaddy 查看更新:)