如何组合两个可变数组?

时间:2012-07-09 14:49:23

标签: iphone ios ipad

我有一个iPhone应用程序,我正在尝试添加两个可变数组。

NSMutableArray *dataArray = [[NSMutableArray alloc] init];
NSMutableArray *dataArray1 = [[NSMutableArray alloc] init];
NSDictionary *news = [dict objectForKey:@"news"];
NSDictionary *deals = [dict objectForKey:@"deals"];
NSLog(@"%@", [news classForCoder]);
NSLog(@"%@", news);

for (NSDictionary *key in news)
{ 
    if ([key isKindOfClass:[NSDictionary class]])
    {
        [dataArray addObject:key];      
    }
}

for (NSDictionary *key in deals)
{
    if ([key isKindOfClass:[NSDictionary class]])
    {
        [dataArray1 addObject:key];        
    }
}

self.newssarray = dataArray;
[self.mTableView reloadData];

这里我想添加两个数组'dataArray'和'dataArray2'组合成一个数组(self.newsarray)。有人可以帮助我实现这个目标吗?

3 个答案:

答案 0 :(得分:24)

NSMutablearray具有所有NSArray方法,包括通过向自身添加另一个数组来创建新数组的方法:

self.newsarray = [[dataArray arrayByAddingObjectsFromArray:dataArray1] mutableCopy];

答案 1 :(得分:4)

如果你的财产是NSMutableArray:

self.newsarray = [NSMutableArray arrayWithArray:[dataArray arrayByAddingObjectsFromArray:dataArray1]];

如果是NSArray:

self.newsarray = [dataArray arrayByAddingObjectsFromArray:dataArray1];

答案 2 :(得分:0)

迅速5。简单而简单的方法

let arrayFind = NSMutableArray()
let arrayNotFind = NSMutableArray()

let arrPicOne = arrayFind as! [Any]
let arrPicTwo = arrayNotFind as! [Any]

let tempResult = arrPicOne + arrPicTwo

let arrResult = NSMutableArray(array: tempResult)