Copy items from NSMutableArray to another

I am trying to copy elements from NSMutableArray to another.

I add elements to their first two MutableArray ( storiesRSS1and 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 works.

After that, I want to mix both in the 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 every element, it is always null!

Could you help me, thanks.

+5
source share
3 answers

NSMutableArrayhas addObjectsFromArray: a method that may be useful to you.

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

On the other hand, you lose memory when you do:

[storiesRSS1/2 addObject:[item copy]];
+4
source

, stories - . , nil, nil.

0

:

stories=nil;
[stories release];
stories=[[NSMutableArray alloc]init];
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++;
}

since it seems like you cannot allocate memory on an Array story. Hope this works. If this is not the case, try printing the stories RSS1 and arrays storyRSS2.

0
source

All Articles