我想将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
}
答案 0 :(得分:18)
您可以使用addObjectsFromArray:类
中的NSMutableArrayall = [[NSMutableArray alloc]init];
[all addObjectsFromArray:animal];
[all addObjectsFromArray:color];
答案 1 :(得分: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];
答案 2 :(得分:1)
我正在使用它:
all = @[animal, color];
但您必须将数组“全部”从NSMutableArray
转换为NSArray
。