简体   繁体   English

如何在Objective C中将两个数组的对象作为对象和键添加到第三个数组

[英]How to add two array's objects as objects and keys to the third array in Objective C

I am trying to add two arrays into the third array ie one array's objects are values to the third array and other array's objects are keys to the third array and I am getting the output as null , Is this a right way to do. 我试图将两个数组添加到第三个数组,即一个数组的对象是第三个数组的值,而另一个数组的对象是第三个数组的键,而我得到的输出为null,这是正确的方法吗?

     NSArray *newcontactkeys,*newcontactvalues ;
     NSMutableArray *autoSyncDataArray;

    newcontactkeys=[self.resultDict objectForKey:@"keys"];
    newcontactvalues=[self.resultDict objectForKey:@"values"];
    [autoSyncDataArray setValue:[self.resultDict objectForKey:@"values"] 
    forKey:[self.resultDict objectForKey:@"Keys"]];

    NSLog(@"autosyncArray is %@",autoSyncDataArray);

Output: autosyncArray is (null) 输出:autosyncArray为(空)

You are using an array, which is an indexed collection, but talking about keys , dictionaries are keyed collections. 您正在使用一个数组,它是一个索引集合,但是说到key ,字典就是键集合。

NSDictionary has a class method to directly create a dictionary from two arrays, use this and your code becomes, updating to modern Obj-C syntax: NSDictionary有一个类方法可以直接从两个数组创建一个字典,使用它,您的代码将更新为现代Obj-C语法:

NSArray *newContactKeys = self.resultDict[@"keys"];
NSArray *newContactValues = self.resultDict[@"values"];
NSDictionary *autoSyncDataDict = [NSDictionary dictionaryWithObjects:newContactValues
                                                             forKeys:newContactsKeys];

HTH HTH

You're not initialising your array. 您没有初始化数组。

NSMutableArray *autoSynchDataArray = [NSMutableArray new]

Also use addObject instead of set value: 还可以使用addObject代替设置值:

[autoSyncDataArray addObject:[self.resultDict objectForKey:@"values"]

You cannot join/Manipulate multiple Array of type NSArray. 您不能加入/操纵多个NSArray类型的Array。 Create a NSMutableArray which allows reordering and manipulation of data in it. 创建一个NSMutableArray,它允许对其中的数据进行重新排序和操作。 You can add any number of objects to it, rearrange it and delete it. 您可以向其中添加任意数量的对象,重新排列并删除它。

NSMutableArray *templateArray = [NSMutableArray new]; NSMutableArray * templateArray = [NSMutableArray new];

There are 2 issues: 有两个问题:

  1. Null issue: You are not initializing array. 空问题:您未初始化数组。 So you will have to use below statement: 因此,您将必须使用以下语句:

    NSMutableArray *autoSyncDataArray = [[NSMutableArray alloc] init]; NSMutableArray * autoSyncDataArray = [[NSMutableArray alloc] init];

  2. Key/Value Pair: If you want to put keys and values in an array. 键/值对:如果要将键和值放入数组中。 It will not work like Dictionary. 它不会像词典那样工作。 It will go into sequential form. 它将采用顺序形式。 If you are ok with sequential form then use: 如果您可以使用顺序表格,请使用:

    [autoSyncDataArray addObject:[self.resultDict objectForKey:@"keys"]]; [autoSyncDataArray addObject:[self.resultDict objectForKey:@“ keys”]]]; [autoSyncDataArray addObject:[self.resultDict objectForKey:@"values"]]; [autoSyncDataArray addObject:[self.resultDict objectForKey:@“ values”]];

otherwise use: 否则使用:

NSMutableDictionary *autoSyncDataDict = [[NSMutableDictionary alloc] init];
[autoSyncDataArray setValue:[self.resultDict objectForKey:@"values"] 
    forKey:[self.resultDict objectForKey:@"Keys"]];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM