8

我想将对象从 2 NSArray 添加到 NSMutableArray。我不知道这件事。

这是我的代码:

@interface ViewController : UITableViewController
{
    NSArray *animal;
    NSArray *color;
    NSMutableArray *all;
}


@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    animal = [[NSArray alloc]initWithObjects:@"Lion",@"Tiger",@"Dog",@"Cat",@"Sheep",@"Wolf", nil];
    color = [[NSArray alloc]initWithObjects:@"Blue",@"Red",@"Yellow",@"Green",@"Black", nil];

    all = ??? ; //how to add object from animal and color array in all 
}
4

4 回答 4

18

您可以使用addObjectsFromArray:NSMutableArray

all = [[NSMutableArray alloc]init];
[all addObjectsFromArray:animal];
[all addObjectsFromArray:color];
于 2013-04-15T10:55:36.290 回答
4

尝试这个:

animal = [[NSArray alloc]initWithObjects:@"Lion",@"Tiger",@"Dog",@"Cat",@"Sheep",@"Wolf", nil];
color = [[NSArray alloc]initWithObjects:@"Blue",@"Red",@"Yellow",@"Green",@"Black", nil];

all = [[NSMutableArray alloc] init];
[all addObjectsFromArray:animal];
[all addObjectsFromArray:color];
于 2013-04-15T10:55:53.617 回答
1

我正在使用这个:

all = @[animal, color];

但是您必须将数组 "all" 从 转换NSMutableArrayNSArray.

于 2014-08-17T08:51:31.140 回答
1

all = [[ all arrayByAddingObjectsFromArray: animal ] mutableCopy];
all = [[ all arrayByAddingObjectsFromArray: color ] mutableCopy];

于 2016-05-12T12:18:49.317 回答