开发者

NSMutableArray replace content?

开发者 https://www.devze.com 2023-01-14 18:55 出处:网络
I have a custom object and NSMutableArray as instance member. I fill the array with some data after creation.

I have a custom object and NSMutableArray as instance member. I fill the array with some data after creation. I need a method to replace the content of the array. I tried:

-(void)replac开发者_C百科eArr:(MyClass*) obj
{
 [mList removeAllObjects];
 NSMutableArray * tempArr=[obj mList];
 mList=[NSMutableArray initWithArray:tempArr];
}

But it is failed on

mList=[NSMutableArray initWithArray:tempArr];


Instead of +alloc-initing another NSMutableArray, you could also just replace the contents of this one by first removing all the objects it contains, and then adding the new ones to it:

- (void)replaceArr:(MyClass *)obj {
    [mList removeAllObjects];
    [mList addObjectsFromArray:[obj mList]];
}


I think you mean [[NSMutableArray alloc] initWithArray:tempArr];

0

精彩评论

暂无评论...
验证码 换一张
取 消