0

在我的应用程序中,我有这样的可变数组中的数据,

   MutableArray1:(
       "22.298166 , 73.165809",
       "22.300598 , 73.167183",
       "22.298101 , 73.166188",
       "22.298128 , 73.166194"
       "22.298130 , 73.166194"
      )

我想将带有数据“22.298130,73.166194”的 NSString 与MutableArray1 的最后三个数据进行比较。

请建议我该怎么做?

4

2 回答 2

1
        if([Array1 count]>3)
        {
           for (int i = [Array1 count] - 4; i < [Array1 count]; i++) {
                if ([[Array1 objectAtIndex:i] isEqualToString:@"22.298130 , 73.166194"]) {
                    NSLog (@"True");
                    //Write your Code Here
                }
            }
        }
于 2012-12-06T04:36:30.487 回答
0
NSArray *array = @[ @"22.298166 , 73.165809",
                    @"22.300598 , 73.167183",
                    @"22.298101 , 73.166188",
                    @"22.298128 , 73.166194",
                    @"22.298130 , 73.166194"];

如果您只需要知道它是否在数组内:

NSString *searchString = @"22.298130 , 73.166194";    
BOOL found = [[array subarrayWithRange:NSMakeRange([array count]-3, 3)] containsObject:searchString];
NSLog(@"%@", (found) ? @"YES" : @"NO");

如果你需要知道索引,你可以做

[[array subarrayWithRange:NSMakeRange([array count]-3, 3)] enumerateObjectsUsingBlock:^(NSString  *obj, NSUInteger idx, BOOL *stop) {
    if ([obj isEqualToString:searchString]) {
        *stop = YES; //avoid further loops, if we had a positive hit.
         NSLog(@"%lu %lu", idx, [array count]-3+idx);  //index in subarray and in original array
    };
}];
于 2012-12-06T05:32:32.730 回答