在iphone中从多个数组添加数组中的对象

时间:2021-08-02 19:43:36

I have 2 array like as follow:

我有2个数组如下:

Array A  ( 

{ title="A",id=1 }, {title="B",id=2},{title="c",id=3}

)

Array B  ( 

{ title="A",id=1 }, {title="B",id=2},{title="c",id=3}

)

Now, I want to combine this arrays & when I try to get the values from combined array, I want know that it is from Array A or Array B.

现在,我想组合这些数组,当我尝试从组合数组中获取值时,我想知道它来自数组A或数组B.

How can I do that?

我怎样才能做到这一点?

Should I use dictionary instead of Array?

我应该使用字典而不是数组吗?

1 个解决方案

#1


3  

Yes, you should use Dictionary instead of Array. Set the Array as an Object with the Key as ArrayName.

是的,您应该使用Dictionary而不是Array。使用Key作为ArrayName将Array设置为Object。

Sample Code :

示例代码:

NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:arrayA,@"ArrayA",arrayB,@"ArrayB", nil];
NSLog(@"dict :: %@",dict);

Update :

To display the title & id in the TableView cell :

要在TableView单元格中显示标题和ID:

yourTitleLabel.text = [[[dict objectForKey:@"ArrayA"] objectAtIndex:indexPath.row] objectForKey:@"title"];
yourIdLabel.text = [[[dict objectForKey:@"ArrayA"] objectAtIndex:indexPath.row] objectForKey:@"id"];

#1


3  

Yes, you should use Dictionary instead of Array. Set the Array as an Object with the Key as ArrayName.

是的,您应该使用Dictionary而不是Array。使用Key作为ArrayName将Array设置为Object。

Sample Code :

示例代码:

NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:arrayA,@"ArrayA",arrayB,@"ArrayB", nil];
NSLog(@"dict :: %@",dict);

Update :

To display the title & id in the TableView cell :

要在TableView单元格中显示标题和ID:

yourTitleLabel.text = [[[dict objectForKey:@"ArrayA"] objectAtIndex:indexPath.row] objectForKey:@"title"];
yourIdLabel.text = [[[dict objectForKey:@"ArrayA"] objectAtIndex:indexPath.row] objectForKey:@"id"];