Iphone 如何将可变数组的最后三个数据与字符串进行比较?
在我的应用程序中,我得到了可变数组中的数据,如下所示Iphone 如何将可变数组的最后三个数据与字符串进行比较?,iphone,objective-c,cocoa-touch,ios4,Iphone,Objective C,Cocoa Touch,Ios4,在我的应用程序中,我得到了可变数组中的数据,如下所示 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:(
"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与可变数组1的最后三个数据进行比较
请告诉我怎么做?if([Array1 count]>3)
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
}
}
}
{
for(int i=[Array1 count]-4;i<[Array1 count];i++){
if([[Array1 objectAtIndex:i]IsequalString:@“22.298130,73.166194”]){
NSLog(@“真”);
//在这里编写代码
}
}
}
如果您只需要知道它是否在阵列中:
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
};
}];
[[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
};
}];