user1283776 user1283776 - 3 months ago 10
Objective-C Question

removeObject works, but addObject does not, both have the same parameters

In the following code, removeObject works fine, but addObject does not work. What is wrong?

NSMutableArray *sourceList = self.questions;
NSMutableArray *randomizedList;

for (NSInteger i = sourceList.count; i > 0; i--) {
//other things

[randomizedList addObject:sourceList[index]];
[sourceList removeObject:sourceList[index]];
}


randomizedList is the same before and after
[randomizedList addObject:sourceList[index]];
is executed:
NSArray > NAObject >isa Class 0x0

Answer

The problem here is that randomizedList is not allocated.

NSMutableArray *sourceList = self.questions;
NSMutableArray *randomizedList = [[NSMutableArray alloc] init];

for (NSInteger i = sourceList.count; i > 0; i--) {
    //other things

    [randomizedList addObject:sourceList[index]];
    [sourceList removeObject:sourceList[index]];
}
Comments