Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSMutableArray removeObjectAtIndex causing unexpected SIGABRT

I have looked at numerous posts which state various ways in which to remove an object from an array correctly, but I am not sure which method is best to use in my instance. I am loading a dictionary from a plist, this dictionary contains numerous arrays, and these arrays contain another dictionary. So I have 3 storage devices setup, 1 to hold the overall dictionary, another for an array, and finally a dictionary to hold the object from the array:

Header:

NSMutableDictionary *questionsDictionary;
NSMutableArray *questionsArray;
NSDictionary *currentQuestion;

@property (nonatomic, retain) NSMutableDictionary *questionsDictionary;
@property (nonatomic, retain) NSMutableArray *questionsArray;
@property (nonatomic, retain) NSDictionary *currentQuestion;

So my first question is to do with the above, are (nonatomic, retain) the right things to use for the following code.

Next I load in my dictionary from the plist:

.m:

NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"MultipleChoice.plist"];
self.questionsDictionary = [[NSDictionary dictionaryWithContentsOfFile:finalPath] retain];

I then setup my question array based upon type based upon my question type:

- (void)setupQuestionType : (NSString *)qType
{
if ([self.questionsDictionary objectForKey:qType])
{
    self.questionsArray = [self.questionsDictionary objectForKey:qType];

    [self pickRandomQuestion];
}
}

Finally (this is where I get the error), I want to grab the a question at random from this question category:

// Pick a random question number based upon amount of questions
int randomQuestionNum = [[NSNumber numberWithInt:(arc4random() % [self.questionsArray count])] intValue];

// Grab the dictionary entry for that question
currentQuestion = [self.questionsArray objectAtIndex:randomQuestionNum];

// Remove the question from the available questions
[self.questionsArray removeObjectAtIndex:randomQuestionNum]; (Error here)

// Set the question text
self.question.text = [currentQuestion objectForKey:kQuestionkey];

Now if I comment out the removeObjectAtIndex line then the code runs fine, and my question is displayed on the screen. This leads me to believe that it isn't a null pointer. So the logical answer points to the fact that self.questionsArray isn't a NSMutableArray. So I tried the following when setting the array:

- (void)setupQuestionType : (NSString *)qType
{
if ([self.questionsDictionary objectForKey:qType])
{
    NSMutableArray *temp = (NSMutableArray *)[self.questionsDictionary objectForKey:qType];
    self.questionsArray = (NSMutableArray *)temp;

    [self pickRandomQuestion];
}
}

Purely to see if I could type_cast it but the error still occurs. Can anyone shed some light on what I'm doing wrong, or the best approach to take?

like image 753
Elliott D'Alvarez Avatar asked Jan 23 '26 10:01

Elliott D'Alvarez


1 Answers

Don't typecast NSArray to NSMutableArray. Instead:

NSArray *temp = [self.questionsDictionary objectForKey:qType];
self.questionsArray = [NSMutableArray arrayWithArray:temp];
// code not tested.
like image 79
Sanjay Chaudhry Avatar answered Jan 26 '26 03:01

Sanjay Chaudhry