如何将MutableArray的最后三个数据与String进行比较?

时间:2012-12-06 04:21:33

标签: iphone objective-c cocoa-touch ios4

在我的应用程序中,我有像这样的Mutable数组中的数据,

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

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

请建议我该怎么做?

2 个答案:

答案 0 :(得分: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
                }
            }
        }

答案 1 :(得分: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
    };
}];