Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 将NSMutableArray中对象的数据与同一密钥组合_Ios_Dictionary_Nsmutablearray_Key - Fatal编程技术网

Ios 将NSMutableArray中对象的数据与同一密钥组合

Ios 将NSMutableArray中对象的数据与同一密钥组合,ios,dictionary,nsmutablearray,key,Ios,Dictionary,Nsmutablearray,Key,我有以下NSMutableArray,其中键为“id,value,name”,键为“id,animal”: <__NSArrayM 0x7a64f450>( { id = ccc; value = 11; name = "Greg"; }, { id = bbb; value = 2; name = "Dan"; }, { i

我有以下NSMutableArray,其中键为“id,value,name”,键为“id,animal”:

    <__NSArrayM 0x7a64f450>(
    {
        id = ccc;
        value = 11;
        name = "Greg";
    },
    {
        id = bbb;
        value = 2;
        name = "Dan";
    },
    {
        id = aaa;
        value = 23;
        name = "Mary";
    },
    {
        id = aaa;
        animal = "dog";
    },
    {
        id = ccc;
        animal = "cat";
    }

)
是否可以通过匹配键来合并内容?所以最后我会:

        <__NSArrayM 0x7a64f450>(
        {
            id = ccc;
            value = 11;
            name = "Greg";
            animal = "cat"
        },
        {
            id = bbb;
            value = 2;
            name = "Dan";
        },
        {
            id = aaa;
            value = 23;
            name = "Mary";
            animal = "dog";
        }

    )

如果关键点与id匹配,我希望合并对象。所有其他关键点都应该合并在一起

> P>您可以考虑使用NSArrays作为对象的NS字典。然后,您可以根据id的大小从数组中访问数据

take one one dictionary from array and use addEntriesFromDictionary for merging them e.g.

NSMutableDictionary *dic=[[NSMutableDictionary alloc] initWithObjects:[NSArray arrayWithObjects:@"23",@"sss", nil] forKeys:[NSArray arrayWithObjects:@"id",@"name", nil]];

NSMutableDictionary *dic1=[[NSMutableDictionary alloc] initWithObjects:[NSArray arrayWithObjects:@"23",@"sss", nil] forKeys:[NSArray arrayWithObjects:@"id",@"animal", nil]];
if([[dic valueForKey:@"id"] isEqualToString:[dic1 valueForKey:@"id"]]){
    [dic addEntriesFromDictionary:dic1];
  }
NSArray* arr = [[NSArray alloc]initWithObjects: value, name, animal];
NSDictionary* dic = [[NSDictionary alloc]init];
[dic addObject:arr forKey:id];

原谅小错误我没有对XCode进行双重检查,因此函数可能有点错误。

更优雅的方法是使用NSPredicate。使用此代码。arrRecords是在合并之前存储所有记录的地方

NSString *strKey;  
NSPredicate *predicate;  
NSArray *tempArray;       

for(int i = 0; i < [arrRecords count]; i++) {
    strKey = [[arrRecords objectAtIndex:i] valueForKey:@"id"];
    predicate = [NSPredicate predicateWithFormat:@"id == %@",strKey];
    tempArray = [arrRecords filteredArrayUsingPredicate:predicate];
    if([tempArray count] > 1) {
       for(int j =0; j < [tempArray count]-1; j++) {
       [[arrRecords objectAtIndex:i] addEntriesFromDictionary:[tempArray objectAtIndex:j+1]];
       [arrRecords removeObject:[tempArray objectAtIndex:j+1]];  // You need to remove the record from super set otherwise this will get duplicated again.
            }
        }
    }
编辑-NSDictionary对象的代码

NSMutableArray *newRecords = [[NSMutableArray alloc] init]; // Add this also before method body

 for(int i = 0; i < [arrRecords count]; i++) {  

     strKey = [[arrRecords objectAtIndex:i] valueForKey:@"id"];  
     predicate = [NSPredicate predicateWithFormat:@"id == %@",strKey];  
     tempArray = [arrRecords filteredArrayUsingPredicate:predicate];  
     if([tempArray count] > 1) {  
         NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] init];   
         for(int j =0; j < [tempArray count]; j++) {  
             [tempDict addEntriesFromDictionary:[tempArray objectAtIndex:j]];  
             if(j!=0) {  
                [arrRecords removeObject:[tempArray objectAtIndex:j]];  
             }                
         }  
         [newRecords addObject:tempDict];
     }
     else {
        [newRecords addObject:[arrRecords objectAtIndex:i]];
     }  
 }

很近。直到[[arrRecords objectAtIndex:i]addEntriesFromDictionary:[tempArray objectAtIndex:j+1]];然后是错误:[[uuuu\NSDictionaryI addEntriesFromDictionary:]:无法识别的选择器发送到实例0x78ef0450'。arrRecords是带有字典的nsmutablearray吗?arrRecords是一个带有NSMutableDictionary的NSMutableArray。这就是它崩溃的原因。我建议你也应该使用可变字典。但是,如果这不可行,请检查编辑后的答案。似乎很有希望,我如何使用我的NSMutableArray定义dic和dic1?