ios - Why does a (copy, nonatomic) NSMutableArray property create NSArrays? -
ios - Why does a (copy, nonatomic) NSMutableArray property create NSArrays? -
i made error while creating tableview class, , accidentally kept @property re-create when defined it:
@property (copy, nonatomic) nsmutablearray *words; i initialised array "correctly": (note 3rd attempt, please ignore fact i'm not using mutablecopy , other improve ways of doing this)
nsarray *fixedwords = @[@"eeny", @"meeny", @"miny", @"moe", @"catch", @"a", @"tiger", @"by", @"his", @"toe"]; nsmutablearray *mutwords = [[nsmutablearray alloc] initwitharray:fixedwords]; self.words = mutwords; however when later came reorder array, crashed on removeobjectatindex line:
id object = [self.words objectatindex:fromindexpath.row]; nsuinteger = fromindexpath.row; nsuinteger = toindexpath.row; [self.words removeobjectatindex:from]; with error message
unrecognized selector sent instance took lot of digging figure out because re-create means assigning nsmutablearray results in creation of standard (nonmutable) nsarray. can explain why right behaviour?
-copy, implemented mutable cocoa classes, returns immutable counterparts. thus, when nsmutablearray sent -copy, returns nsarray containing same objects.
because words has memory qualifier copy, line:
nsmutablearray *mutwords = [[nsmutablearray alloc] initwitharray:fixedwords]; self.words = mutwords; expands out to:
nsmutablearray *mutwords = [[nsmutablearray alloc] initwitharray:fixedwords]; self.words = [mutwords copy]; given nsmutablearray subclass of nsarray, compiler doesn't complain, , have ticking time bomb on hands because nsarray not recognize it's mutable subclass' methods (because cannot mutate it's contents).
ios objective-c nsmutablearray nsarray
Comments
Post a Comment