如何根据以下示例将数组添加到Objective c中的另一个数组?

时间:2017-10-17 10:05:10

标签: ios objective-c arrays nsmutablearray nsarray

这是一个代码,

NSMutableArray *list = [NSMutableArray arrayWithObjects:
                           [MagnetKeyValuePair keyValuePairWithKeyAndValue:@"test1" value:@"Q test"],
                           [MagnetKeyValuePair keyValuePairWithKeyAndValue:@"test2" value:@"W test"],
                           [MagnetKeyValuePair keyValuePairWithKeyAndValue:@"test3" value:@"E Test"],
                       nil];

所以这里,数组存储键和值。

我正在使用Library for Picker视图。这是链接 https://github.com/ufukk/MagnetPopupPicker

enter image description here

所以,我想传递我自己的值,其形式为id和name,

它可以是NSArray和NSMutableArray

e.g NSMutableArray * name= [NSMutableArray arrayWithObjects:@"Aashu",@"Kaveri",@"Rutuja",nil];
    NSMutableArray * id= [NSMutableArray arrayWithObjects:@"1",@"2",@"3",nil];

所以我想添加另一个NSMutableArray。所以,在uipickerview中,我看到我的朋友的名字就像下面的图片一样。

enter image description here

代码是,对于此图像

self.button = [[MagnetPopupPickerButton alloc] initWithFrame:CGRectMake(170, 50, 150, 30)];
    self.button.backgroundColor = [UIColor darkGrayColor];
    self.button.popoverColor = [UIColor darkGrayColor];
    [self.button setTitle:@"Select" forState:UIControlStateNormal];

    NSArray *list = [NSArray arrayWithObjects:[MagnetKeyValuePair keyValuePairWithKeyAndValue:@"test1" value:@"Aashu"],
                     [MagnetKeyValuePair keyValuePairWithKeyAndValue:@"test2" value:@"Kaveri"],
                     [MagnetKeyValuePair keyValuePairWithKeyAndValue:@"test3" value:@"Rutuja"],
                     nil];

    [self.button setOptions:list keyNames:[MagnetKeyValuePair keyValuePairWithKeyAndValue:@"key" value:@"value"]];
    [self.view addSubview:self.button];

1 个答案:

答案 0 :(得分:0)

  1. 你不应该使用两个数组。这很容易导致错误。使用一系列词典。
  2. 使用addObjectsFromArray