FilteredArrayUsingPredicate 不起作用
我正在尝试将filteredArrayUsingPredicate 与根据.plist 文件中的数据构建的数组一起使用。不知怎的,它似乎永远不会过滤我的数组。
这就是我的数组的构建方式:
DrillDownAppAppDelegate *AppDelegate = (DrillDownAppAppDelegate *)[[UIApplication sharedApplication] delegate];
self.tableDataSource = [AppDelegate.data objectForKey:@"Rows"];
copyDataSource = [ tableDataSource mutableCopy];
然后我的谓词是这样的,
NSString *searchFor = search.text;
[tableDataSource release];
tableDataSource = [copyDataSource mutableCopy];
if ([searchFor length] > 0) {
NSLog(@"array = %@",tableDataSource);
NSPredicate *pred = [NSPredicate predicateWithFormat:@"Self beginswith[c] %@",searchFor];
[tableDataSource filteredArrayUsingPredicate:pred];
}
I'm trying to use filteredArrayUsingPredicate with an array that has been built from data in a .plist file. Somehow it never seems to filter my array.
this is how my array is built:
DrillDownAppAppDelegate *AppDelegate = (DrillDownAppAppDelegate *)[[UIApplication sharedApplication] delegate];
self.tableDataSource = [AppDelegate.data objectForKey:@"Rows"];
copyDataSource = [ tableDataSource mutableCopy];
and then my predicate goes like this,
NSString *searchFor = search.text;
[tableDataSource release];
tableDataSource = [copyDataSource mutableCopy];
if ([searchFor length] > 0) {
NSLog(@"array = %@",tableDataSource);
NSPredicate *pred = [NSPredicate predicateWithFormat:@"Self beginswith[c] %@",searchFor];
[tableDataSource filteredArrayUsingPredicate:pred];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
-[NSArray FilterUsingPredicate:]
返回一个新数组,其中包含就地过滤-[NSMutableArray filterUsingPredicate:]
。因此请改用以下内容:-[NSArray filteredArrayUsingPredicate:]
returns a new array that contains the results-[NSMutableArray filterUsingPredicate:]
filters in-place.So use the following instead:
我认为您需要在替换到 predicateWithFormat: 方法中的字符串周围加上引号。即:
NSPredicate *pred = [NSPredicate predicateWithFormat:@"self beginwith[c] \"%@\"",searchFor];
还可以尝试使用“self”一词全部小写,就像关键字“self”一样,并记住对象的实例实际上应该以小写字母开头。例如,“appDelegate”。
I think you need quotes round the string your subsituting into the predicateWithFormat: method. i.e:
NSPredicate *pred = [NSPredicate predicateWithFormat:@"self beginswith[c] \"%@\"",searchFor];
Also try using the word "self" as all lower case, as in the keyword "self", and remember that instances of objects should really be started with a lowercase letter. For example, "appDelegate".