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

Posted

技术标签:

【中文标题】根据我的以下示例,如何将数组添加到 Objective c 中的另一个数组?【英文标题】:how to add array to another array in Objective c, according to my following example? 【发布时间】:2017-10-17 10:05:09 【问题描述】:

这是一个代码,

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

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

可以是 NSArray 和 NSMutableArray

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

所以我想添加另一个 NSMutableArray。所以,在 uipickerview 我看到我朋友的名字如下图。

代码是,对于这张图片

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】:
    您不应该使用两个数组。这很容易导致错误。使用字典数组。 使用addObjectsFromArray

【讨论】:

是的,你是对的,但是我使用的代码只接受数组。这是代码,[self.button setOptions:list keyNames:[MagnetKeyValuePair keyValuePairWithKeyAndValue:@"key" value:@"value"]];在这段代码中,setOptions 接受 NSArray。 NSMutableArrayNSArray 的子类,因此它应该可以开箱即用。您始终可以使用copyNSMutableArray 的实例中获取NSArray 的实例。

以上是关于根据我的以下示例,如何将数组添加到 Objective c 中的另一个数组?的主要内容,如果未能解决你的问题,请参考以下文章

如何将嵌套数组添加到我的模型类

如何按值而不是按引用将数组添加到列表中?

将数组添加到父组件的状态

如何根据从 ajax 调用返回的数据动态地将选项添加到我的自动完成功能?

将 Composer 包添加到 Laravel Providers 数组

如何在 Objective C 中形成 JSON 格式的可变数组