2

I have an iPhone application in which i am trying to add two mutable arrays.

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];

Here I want to add the two arrays 'dataArray' and 'dataArray2' to be combined into one array (self.newsarray). Can anybody help me in achieving this?

3 Answers 3

24

NSMutable array has all of NSArray's methods, including one for creating a new array by adding another array to itself:

self.newsarray = [[dataArray arrayByAddingObjectsFromArray:dataArray1] mutableCopy];
Sign up to request clarification or add additional context in comments.

7 Comments

thanks for the reply,But it is giving me a warning incompatable pointer types from nsmutable array to nsarray?
I assume you mean that the @property is expects to be mutable; answer modified.
self.newsarray = [[dataArray arrayByAddingObjectsFromArray:dataArray1] mutableCopy]; happy now....
Good call; SO has thought of everything ;-)
One thing to watch out for with this method is that if dataArray is nil you'll end up with the new array being nil even if dataArray1 isn't nil or empty.
|
4

If your property is NSMutableArray :

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

If it is NSArray :

self.newsarray = [dataArray arrayByAddingObjectsFromArray:dataArray1];

Comments

0

Swift 5 . Easy ans simple way

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.