Search through NSArray for string

后端 未结 3 703
心在旅途
心在旅途 2020-12-13 02:56

I would like to search through my NSArray for a certain string.

Example:

NSArray has the objects: \"dog\", \"cat\", \"fat dog\", \"thing\", \"another thing\"

3条回答
  •  独厮守ぢ
    2020-12-13 03:21

    Not tested so might have a syntax error, but you'll get the idea.

    NSArray* inputArray = [NSArray arrayWithObjects:@"dog", @"cat", @"fat dog", @"thing", @"another thing", @"heck here's another thing", nil];
    
    NSMutableArray* containsAnother = [NSMutableArray array];
    NSMutableArray* doesntContainAnother = [NSMutableArray array];
    
    for (NSString* item in inputArray)
    {
      if ([item rangeOfString:@"another"].location != NSNotFound)
        [containsAnother addObject:item];
      else
        [doesntContainAnother addObject:item];
    }
    

提交回复
热议问题