【问题标题】:how to capture the first letter of each array entry如何捕获每个数组条目的第一个字母
【发布时间】:2012-11-06 21:53:45
【问题描述】:

我有一个 NSString 值的排序数组,我想知道如何仅在第一个字母不同时捕获每个字符串的第一个字母并将其放入新的 NSArray。

例如,如果我有一个类似的数组

"a, aaa, aaaa, b, c, d, dd, ddd"

在新的 NSArray 中应该是这样的

"a, b, c, d"

任何帮助将不胜感激。

【问题讨论】:

  • 创建一个空的 mutableArray。遍历排序后的字母,跟踪 previousFirstLetter 并将其与当前字母进行比较,如果不同,则将该字母添加到 mutableArray 并更新 previousFirstLetter。

标签: ios nsarray


【解决方案1】:

类似这样的:

- (NSArray *)indexLettersForStrings:(NSArray *)strings {
    NSMutableArray *letters = [NSMutableArray array];
    NSString *currentLetter = nil;
    for (NSString *string in strings) {
        if (string.length > 0) {
            NSString *letter = [string substringToIndex:1];
            if (![letter isEqualToString:currentLetter]) {
                [letters addObject:letter];
                currentLetter = letter;
            }
        }
    }
    return [NSArray arrayWithArray:letters];
}

【讨论】:

  • 对迟到的回复感到抱歉.. 尝试了很多东西.. 然后看到这个尝试了它并且它工作得很好!非常感谢:)
【解决方案2】:

NSString+字母索引.h

@interface NSString (LetterIndex)
@property (nonatomic, readonly) NSString * firstLetter;
@end

NSString+LetterIndex.m

@implementation NSString (LetterIndex)

- (NSString *)firstLetter
{
    return self.length ? [self substringToIndex:1] : @"";
}

你的方法:

- (NSArray *)indexLettersForStrings:(NSArray *)strings {
    NSSet * distinctValues = [NSSet setWithArray:[strings valueForKey:@"firstLetter"]];
    return [[distinctValues allObjects] sortedArrayUsingSelector:@selector(compare:)]
}

另外,如果你有一些自定义类的对象,并且想通过某个字符串参数的首字母对它们进行分组,你可以使用这个:

NSSet * distinctValues = [NSSet setWithArray:[objects valueForKeyPath:@"myStringParam.firstLetter"]];

【讨论】:

    【解决方案3】:
    NSArray *array = [NSArray arrayWithObjects:@"a", @"aaa", @"aaaa",@"b", @"c", @"d", @"dd", @"ddd", nil];
    BOOL control = YES;
    NSMutableArray *array2 = [NSMutableArray array];
    for (int i = 0; i<array.count; i++) {
        for (int j = 0; j<array2.count;j++){
            if ([[array2 objectAtIndex:j]isEqualToString:[[array objectAtIndex:i]substringToIndex:1]]){
                control = NO;
            }
            else
                control = YES;
        }
        if (control)
            [array2 addObject:[[array objectAtIndex:i]substringToIndex:1]];
    }
    

    【讨论】:

      【解决方案4】:

      试试这个:

      NSArray *arr = @[@"a", @"aaa", @"aaaa", @"b", @"c", @"d", @"dd", @"ddd"];
      NSMutableArray *newArr = [NSMutableArray array];
      NSMutableSet *set = [NSMutableSet set];
      
      for (NSString *str in arr)
      {
          if (![set containsObject:[str substringToIndex:1]])
              [newArr addObject:[str substringToIndex:1]];
          [set addObject:[str substringToIndex:1]];
      
      }
      
      NSLog(@"%@", newArr);
      

      这使用一个 Set 来跟踪已经过去抛出的事件。当它不存在时,它会将它们放入一个新数组中。

      【讨论】:

      • 使用containsObject: 然后使用addObject:NSSet 是多余的。他们还需要NSArray 作为最终结果。请参阅下面的答案。
      • 他确实要求将答案放在新数组中
      猜你喜欢
      • 2017-11-25
      • 1970-01-01
      • 2022-08-23
      • 1970-01-01
      • 1970-01-01
      • 2020-09-25
      • 2014-12-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多