ios – 通过字符串属性的第一个字母过滤数组

ios – 通过字符串属性的第一个字母过滤数组,第1张

概述我有一个NSArray与具有name属性的对象. 我想按名称过滤数组 NSString *alphabet = [agencyIndex objectAtIndex:indexPath.section]; //---get all states beginning with the letter--- NSPredicate *predicate = [NSPredicat 我有一个NSArray与具有name属性的对象.

我想按名称过滤数组

Nsstring *Alphabet = [agencyIndex objectAtIndex:indexPath.section];    //---get all states beginning with the letter---    nspredicate *predicate =    [nspredicate predicateWithFormat:@"SELF beginswith[c] %@",Alphabet];    NSMutableArray *ListSimpl = [[NSMutableArray alloc] init];    for (int i=0; i<[[Database sharedDatabase].agents count]; i++) {        Town *_town = [[Database sharedDatabase].agents objectAtIndex:i];        [ListSimpl addobject:_town];    }    NSArray *states = [ListSimpl filteredArrayUsingPredicate:predicate];

但是我收到一个错误 – “不能做一个不是字符串的东西的子串 *** 作(lhs =< 1,Arrow> rhs = A)”

我该怎么做?我想过滤名字中第一个字母的数组为’A’.

解决方法 尝试以下代码
nspredicate *pred = [nspredicate predicateWithFormat:@"SELF like %@",yourname];NSArray *filteredArr = [yourArray filteredArrayUsingPredicate:pred];

编辑:

nspredicate模式应该是:

nspredicate *pred =[nspredicate predicateWithFormat:@"name beginswith[c] %@",Alphabet];
总结

以上是内存溢出为你收集整理的ios – 通过字符串属性的第一个字母过滤数组全部内容,希望文章能够帮你解决ios – 通过字符串属性的第一个字母过滤数组所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1113593.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存