Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy items from NSMutableArray to another

I try to copy items from NSMutableArray to another.

I add items in my two first MutableArray (storiesRSS1 and storiesRSS2):

[item setObject:currentTitle forKey:@"title"];
[item setObject:currentLink forKey:@"link"];
[item setObject:currentSummary forKey:@"summary"]; 
[item setObject:currentDate forKey:@"date"];
[item setObject:currentImage forKey:@"image"];
[item setObject:currentMovie forKey:@"movie"];
[storiesRSS1 addObject:[item copy]];


[item setObject:currentTitle forKey:@"title"];
[item setObject:currentLink forKey:@"link"];
[item setObject:currentSummary forKey:@"summary"]; 
[item setObject:currentDate forKey:@"date"];
[item setObject:currentImage forKey:@"image"];
[item setObject:currentMovie forKey:@"movie"];
[storiesRSS2 addObject:[item copy]];

For these two arrays it work.

After that I want to mix both in a third array (stories).

int i=0;
int nbElement=[storiesRSS1 count]+[storiesRSS2 count];
while (i<nbElement) {
    if (i<[tempArrayTed count]) {
        [stories addObject:[storiesRSS1 objectAtIndex:i]];
    }
    if (i<[tempArrayYoutube count]) {
        [stories addObject:[storiesRSS2 objectAtIndex:i]];
    }
    i++;
}

But when I try to show each items, it's always null!

Could you help me, thanks.

like image 825
Guillaume Avatar asked Oct 11 '22 10:10

Guillaume


1 Answers

NSMutableArray has an addObjectsFromArray: method that you might find useful.

[stories addObjectsFromArray:storiesRSS1];
[stories addObjectsFromArray: storiesRSS2];

On a side note, you're leaking memory when you do:

[storiesRSS1/2 addObject:[item copy]];
like image 72
Tom Irving Avatar answered Oct 28 '22 12:10

Tom Irving