objective c - Why mutable object(NSMutableArray and NSMutableDictionary) required alloc- init?while we can directly assign value to immutable objects -


//mutable object...   //first initialize  nsmutablearray *arr=[[nsmutablearray alloc]init];   //then add value  [arr addobjects:@"iphone",@"android",nil];     //but can assign value immutable array without initialize  nsarray *brr=@[@"iphone",@"android"]; 

there many ways initialize nsmutablearray. example:

nsmutablearray *arr = [nsmutablearray arraywithobjects: @"iphone", @"android", nil]; 

the @[] literal syntax new addition objc, , returns nsarray (non-mutable), why can't assign directly nsmutablearray. it's syntactic sugar [nsarray arraywithobjects:count:]. if want use syntax, still can, though:

nsarray *brr = [@[@"iphone",@"android"] mutablecopy]; 

Comments