Obj-c - 如何检查 NSString 是否包含一个或另一个?
Obj-c - How can I check if NSString contains one OR the other?
我想检查我的 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];
知道这应该是什么样子吗?我似乎无法正确使用语法。问题是 'filtered' 不返回字符串名称包含 "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
结合会有帮助?
NSPredicate *p1 = [NSPredicate predicateWithFormat:@"name contains[cd] %@", @"Brittany"];
NSPredicate *p2 = [NSPredicate predicateWithFormat:@"name contains[cd] %@", @"Bob"];
NSCompoundPredicate *p = [NSCompoundPredicate orPredicateWithSubpredicates:@[p1, p2]];
NSArray *filtered = [self.messages filteredArrayUsingPredicate:p];
我用swift语言写的。尝试转换成 Objective c。
var str = ["Hello", "hel","hello"]
let predicate1 = NSPredicate(格式: "SELF contains %@","Hello")
let predicate2 = NSPredicate(格式: "SELF contains %@","helo")
let predicatecompound = NSCompoundPredicate.init(type: .or, subpredicates: [predicate1,predicate2])
print(str.filter { predicatecompound.evaluate(with: $0) })
参考:
Combining 'AND' and 'OR' Condition in NSPredicate
我想检查我的 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];
知道这应该是什么样子吗?我似乎无法正确使用语法。问题是 'filtered' 不返回字符串名称包含 "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
结合会有帮助?
NSPredicate *p1 = [NSPredicate predicateWithFormat:@"name contains[cd] %@", @"Brittany"];
NSPredicate *p2 = [NSPredicate predicateWithFormat:@"name contains[cd] %@", @"Bob"];
NSCompoundPredicate *p = [NSCompoundPredicate orPredicateWithSubpredicates:@[p1, p2]];
NSArray *filtered = [self.messages filteredArrayUsingPredicate:p];
我用swift语言写的。尝试转换成 Objective c。 var str = ["Hello", "hel","hello"]
let predicate1 = NSPredicate(格式: "SELF contains %@","Hello") let predicate2 = NSPredicate(格式: "SELF contains %@","helo") let predicatecompound = NSCompoundPredicate.init(type: .or, subpredicates: [predicate1,predicate2]) print(str.filter { predicatecompound.evaluate(with: $0) })
参考: Combining 'AND' and 'OR' Condition in NSPredicate