Search through NSArray for string

后端 未结 3 705
心在旅途
心在旅途 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:33

    If the strings inside the array are known to be distinct, you can use sets. NSSet is faster then NSArray on large inputs:

    NSArray * inputArray = [NSMutableArray arrayWithObjects:@"one", @"two", @"one again", nil];
    
    NSMutableSet * matches = [NSMutableSet setWithArray:inputArray];
    [matches filterUsingPredicate:[NSPredicate predicateWithFormat:@"SELF contains[c] 'one'"]];
    
    NSMutableSet * notmatches = [NSMutableSet setWithArray:inputArray];
    [notmatches  minusSet:matches];
    

提交回复
热议问题