简体   繁体   中英

NSMutableArray adding or removing

here i have a mutable array in one vc pass in 2nd vc with some values.

I need to change values in self.namesFoodSubCategory problem is it also change values in first vc array.

Now if i change it to self.namesFoodSubCategory = [namesArray copy] ; now it copy well but i cannot add or remove elements in self.namesFoodSubCategory it produce crash.

can anybody help me????

 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil andParamArray:(NSMutableArray *)namesArray
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.namesFoodSubCategory = namesArray ;

        NSLog(@"In Sec   %@",self.namesFoodSubCategory);

    }
    return self;
}

The copy method creates immutable copy of object. To preserve mutability you need to use -mutableCopy instead or convenience constructor (assuming you use ARC, so no need for autorelease etc):

self.namesFoodSubCategory = [namesArray mutableCopy];

or

self.namesFoodSubCategory = [MSMutableArray arrayWithArray:namesArray];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM