【问题标题】:Filter Search through NSArray by letter not working. IOS按字母过滤 NSArray 搜索不起作用。 IOS
【发布时间】:2014-08-07 08:50:45
【问题描述】:

我正在尝试使用我的UITextfeild.text 中的文本过滤/搜索NSArray 如果我尝试搜索匹配字符串,过滤器会起作用,但当我在文本字段中键入字母时,过滤后的数组总是为空。

NSArray *testArray  = [NSArray arrayWithObjects:@"testmailone@test.com",@"a@test.com",@"b@testing.com",@"h@fgdfgd",@"helloMan@test.com", @"dfsdfsdfsdfsdfs@dsfdfs",nil];
NSPredicate *predicatedString = [NSPredicate predicateWithFormat:@"SELF == %@", self.addHungryPersonTF.text];
NSArray *resultsArrayString = [testArray filteredArrayUsingPredicate:predicatedString];
NSLog(@"resultsArrayString%@",resultsArrayString);

这将返回 null 但如果我更改字符串以匹配另一个设置的字符串,如下所示:

NSString *matches = [NSString stringWithFormat:@"testmailone@test.com"];

NSLog
resultsArrayString(
"testmailone@test.com"
)

然后它会在数组中填充匹配的字符串。

有人知道为什么它不会按字母搜索我的NSArray吗?当我输入它应该正确地检查数组?

【问题讨论】:

  • 也许在你的谓词中尝试使用LIKE 而不是==
  • 您的示例代码甚至无法编译。请提供真实的代码和输出。

标签: ios ios7 nsarray nspredicate


【解决方案1】:
NSPredicate *predicatedString = [NSPredicate predicateWithFormat:@"SELF CONTAINS %@", self.addHungryPersonTF.text];
//keep this one for get all the strings which contains the substring or string itself

   NSPredicate *predicatedString = [NSPredicate predicateWithFormat:@"SELF BEGINS %@", self.addHungryPersonTF.text];
//keep this one for sting starts with the key word

【讨论】:

    猜你喜欢
    • 2015-03-12
    • 1970-01-01
    • 1970-01-01
    • 2016-07-29
    • 2018-08-23
    • 2018-11-28
    • 2016-08-04
    • 2013-09-24
    • 1970-01-01
    相关资源
    最近更新 更多