我是iOS的新手,我面临着比较两个阵列的问题。我试过这段代码
if ([arr1 isEqualToArray:arr2])
{
NSLog(@"Print the output to update...");
}
但这对我不起作用。因为我的阵列就像这样
arr1=[@"1",@"2",@"3",@"4",@"5",@"6"];
arr2=[@"2"];
所以,我尝试了像这样的代码
NSSet *set1 = [NSSet setWithArray:arr1];
NSSet *set2 = [NSSet setWithArray:arr2];
if ([set1 isEqualToSet:set2]) {
// equal
}
但是,这对我不起作用。在我的情况下,arr1来自web服务,而arr2来自核心数据。你建议任何其他建议来比较这两个数组。
如果条件我正在更新我的代码,而在其他情况下我正在插入
if([arr1 isEqualToArray:arr2])
{
NSManagedObjectContext *context = [self managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"EntityName" inManagedObjectContext:context]];
NSError *error = nil;
NSArray *results = [context executeFetchRequest:request error:&error];
NSManagedObject* favoritsGrabbed = [results objectAtIndex:0];
[favoritsGrabbed setValue:@"1" forKey:@"Key"];
// Save the object to persistent store
if (![context save:&error]) {
NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}
}
else
{
if (self.device) {
// Update existing device
[device setValue:Audit forKey:@"key1"];
[device setValue:MethodID forKey:@"key2"];
[device setValue:CheckPointID forKey:@"key3"];
[device setValue:GlobalStringChk forKey:@"key4"];
[device setValue:RegionID forKey:@"key5"];
[device setValue:BranchID forKey:@"key6"];
[device setValue:SiteID forKey:@"key7"];
[device setValue:AuID forKey:@"key8"];
[device setValue:userid forKey:@"key9"];
[device setValue:StringIndex forKey:@"key10"];
} else {
// Create a new device
NSManagedObject *newDevice = [NSEntityDescription insertNewObjectForEntityForName:@"EntityName" inManagedObjectContext:context];
[newDevice setValue:Audit forKey:@"key1"];
[newDevice setValue:MethodID forKey:@"key2"];
[newDevice setValue:CheckPointID forKey:@"key3"];
[newDevice setValue:GlobalStringChk forKey:@"key4"];
[newDevice setValue:RegionID forKey:@"key5"];
[newDevice setValue:BranchID forKey:@"key6"];
[newDevice setValue:SiteID forKey:@"key7"];
[newDevice setValue:AuID forKey:@"key8"];
[newDevice setValue:userid forKey:@"key9"];
[newDevice setValue:StringIndex forKey:@"key10"];
}
}
听到,我需要比较数组,以便我可以更新核心数据中的值,如果数组不相等,那么我需要插入它们。所以,我不能使用循环。如果我使用循环,请查看其他条件它插入数据直到循环运行,我想在click上插入一个值。所以,我不能使用循环。
答案 0 :(得分:1)
如果您想要确定一个阵列中的元素是否存在于另一个阵列中,您可以尝试以下
NSArray * firstArray = @[@"1",@"2",@"3",@"4"];
NSArray * secondArray = @[@"2",@"10",@"20"];
for(int index = 0 ; index < [firstArray count] ; index++)
{
NSString * element = [firstArray objectAtIndex:index];
if([secondArray containsObject:element])
{
//do stuff
//This block will execute only when element == @"2"
}
}
答案 1 :(得分:1)
for (int i = 0; i < array2.count; i++)
{
if ([array1 containsObject:[array2 objectAtIndex:i]])
{
NSLog(@"Array1 contains array2 object");
}
else
{
NSLog(@"Array1 do not contains array2 object");
}
}
你可以试试这个。这对我提供上述数据非常有用。
答案 2 :(得分:0)
这有很多方法。
使用嵌套循环来了解相等元素的确切位置:
for(int i= 0; i<arr2.count; i++){
for(int j= 0; j<arr1.count; j++){
if(arr1[j] == arr2[i])
NSLog(@"index position: %d element %@:", j, array1[j]);
}
}
或者您可以使用containsObject:
方法:
containsObject :返回一个布尔值,指示给定对象是否存在于数组中。
for(int i= 0; i<arr2.count; i++){
if ([arr1 containsObject:[arr2 objectAtIndex:i]]) {
// indicates whether a given object is present in the array.
}
}
答案 3 :(得分:0)
试试这个
arr1=[@"1",@"2",@"3",@"4",@"5",@"6"];
arr2=[@"2"];
//compare result
BOOL bol = (arr1.count == arr2.count);
// if count equal
if (bol) {
for (NSInteger i = 0; i < arr1.count; i++) {
NSString *str1 = [arr1 objectAtIndex:i];
NSString *str2 = [arr2 objectAtIndex:i];
if (![str1 isEqualToString:str2]) {
bol = NO;
break;
}
}
}
或使用块
NSArray *arr1=@[@"1",@"2",@"3",@"4",@"5",@"6"];
NSArray *arr2=@[@"2"];
//compare result
__block BOOL bol = (arr1.count == arr2.count);
// if count equal
if (bol) {
[arr1 sortedArrayUsingComparator:^NSComparisonResult(NSString *obj1, NSString *obj2) {
if (![obj1 isEqualToString:obj2]) {
bol = NO;
}
return NSOrderedAscending;
}];
}
答案 4 :(得分:0)
我的评论和标题我想:
如果这是正确的,那么改变你的行:
if([arr1 isEqualToArray:arr2])
到
if([arr1 containsObject:arr2[0]])
检查arr1
是否包含arr2
的第一个元素(即arr2[0]
)。
HTH