2

我有一个 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)。任何人都可以帮助我实现这一目标吗?

4

3 回答 3

24

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

self.newsarray = [[dataArray arrayByAddingObjectsFromArray:dataArray1] mutableCopy];
于 2012-07-09T14:51:48.253 回答
4

如果您的属性是 NSMutableArray :

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

如果是 NSArray :

self.newsarray = [dataArray arrayByAddingObjectsFromArray:dataArray1];
于 2012-07-09T15:15:50.807 回答
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)
于 2019-08-07T07:34:45.433 回答