如何在不使用循环 objective c 的情况下比较两个动态数组

How to compare two dynamic array in without using loop objective c

我是 iOS 的新手,我遇到了一个关于比较两个数组的问题。我试过这段代码

 if ([arr1 isEqualToArray:arr2])
 {
      NSLog(@"Print the output to update...");
 }

但这不适用于 me.Because 我的数组是这样的

arr1=[@"1",@"2",@"3",@"4",@"5",@"6"];
arr2=[@"2"];

所以,我尝试了这样的代码

NSSet *set1 = [NSSet setWithArray:arr1];
NSSet *set2 = [NSSet setWithArray:arr2];

if ([set1 isEqualToSet:set2]) {
    // equal
}

但是,这不适用于 me.In 我的案例 arr1 来自网络服务而 arr2 来自核心 data.can 你建议任何其他建议来比较这两个数组。

在 if 条件下我正在更新我的代码,在 else 条件下我正在插入

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"];
                    }
}

听说,我需要比较数组以便我可以更新核心数据中的值,如果数组不相等则我需要插入 them.So,我不能使用 loop.Please 查看它else 条件如果我使用循环它插入数据直到循环运行并且我想在 click.So 处插入一个值,我不能使用循环。

如果您想确定一个数组中的元素是否存在于另一个数组中,您可以尝试以下方法

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"
    }
}

这里有很多方法。

使用嵌套循环了解相等元素的确切位置:

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: Returns 一个布尔值,指示给定对象是否存在于数组中。

for(int i= 0; i<arr2.count; i++){
   if ([arr1 containsObject:[arr2 objectAtIndex:i]]) {
       // indicates whether a given object is present in the array.
   }
}
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");
        }
    }

你可以试试这个。这对我有用你上面提供的数据。

试试这个

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;
    }];
}

从你的评论和标题我猜:

  1. "compare" 和 "is equal" 你的意思是测试 会员资格 ;和
  2. 你的第二个数组只包含一项(所以不需要循环)

如果这是正确的,请更改您的行:

if([arr1 isEqualToArray:arr2])

if([arr1 containsObject:arr2[0]])

检查 arr1 是否包含 arr2 的第一个元素(即 arr2[0])。

HTH