【问题标题】:Filter array by first letter of string property按字符串属性的第一个字母过滤数组
【发布时间】:2013-09-13 21:50:41
【问题描述】:

我有一个NSArray,其中的对象具有name 属性。

我想通过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 = rhs = A)”

我该怎么做?我想过滤name 中第一个字母为“A”的数组。

【问题讨论】:

标签: ios objective-c cocoa-touch filter nsarray


【解决方案1】:

试试下面的代码

NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF like %@", yourName];
NSArray *filteredArr = [yourArray filteredArrayUsingPredicate:pred];

已编辑:

NSPredicate 模式应该是:

NSPredicate *pred =[NSPredicate predicateWithFormat:@"name beginswith[c] %@", alphabet];

【讨论】:

  • 不工作(错误 - 不能对不是字符串的东西进行子字符串操作(lhs = rhs = А)
  • @please 把你的数组列表告诉我你想要什么?
  • @Nubaslon- 更改 NSPredicate 模式,例如 SELF beginswith[c] %@
  • 数组包含 Town 类的对象,Town 有属性 - id, name
  • 那么NSLog(@"%@",listSimpl);是什么
【解决方案2】:

这里是 NSPredicate 过滤数组的基本用法之一。

NSMutableArray *array =
[NSMutableArray arrayWithObjects:@"Nick", @"Ben", @"Adam", @"Melissa", @"arbind", nil];

NSPredicate *sPredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] 'b'"];
NSArray *beginWithB = [array filteredArrayUsingPredicate:sPredicate];
NSLog(@"beginwithB = %@",beginWithB);

【讨论】:

    【解决方案3】:

    NSArray 提供了另一个排序数组的选择器:

    NSArray *sortedArray = [array sortedArrayUsingComparator:^NSComparisonResult(Person *first, Person *second) {
        return [first.name compare:second.name];
    }];
    

    【讨论】:

      【解决方案4】:

      如果你想过滤数组看看这段代码:

      NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == %@", @"qwe"];
      NSArray *result = [self.categoryItems filteredArrayUsingPredicate:predicate];
      

      但是如果你想排序数组看看下面的函数:

      - (NSArray *)sortedArrayUsingFunction:(NSInteger (*)(id, id, void *))comparator context:(void *)context;
      - (NSArray *)sortedArrayUsingFunction:(NSInteger (*)(id, id, void *))comparator context:(void *)context hint:(NSData *)hint;
      - (NSArray *)sortedArrayUsingSelector:(SEL)comparator;
      

      【讨论】:

        【解决方案5】:

        访问https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Collections/Articles/Arrays.html

        使用这个

        [listArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
        

        【讨论】:

          【解决方案6】:

          查看这个库

          https://github.com/BadChoice/Collection

          它带有许多简单的数组函数,再也不用写循环了

          所以你可以这样做

          NSArray* result = [thArray filter:^BOOL(NSString *text) {
              return [[name substr:0] isEqualToString:@"A"]; 
          }] sort];
          

          这只会获取以字母顺序排序的以 A 开头的文本

          如果你是用对象做的:

          NSArray* result = [thArray filter:^BOOL(AnObject *object) {
              return [[object.name substr:0] isEqualToString:@"A"]; 
          }] sort:@"name"];
          

          【讨论】:

            猜你喜欢
            • 2011-03-22
            • 1970-01-01
            • 2019-04-25
            • 2022-05-22
            • 1970-01-01
            • 2012-11-24
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多