0

I'm getting through the Fall 2010 version of the Stanford class CS193P, iPhone programming. On assignment 2 I'm improving upon a calculator app created in assignment one. It seems that I'm almost finished but the app crashes when I try to press a variable located on the interface (for these purposes, "x").

Using my limited debugging skills, I managed to track the problem down. The problem comes in the method "(NSSet)variablesInExpression:(id)anExpression".

+ (NSSet *)variablesInExpression:(id)anExpression
{
NSMutableSet *setOfVariables = [[NSSet alloc] init];
for (NSString *str in anExpression) {
    if ([str hasPrefix:VARIABLE_PREFIX]) {
        [setOfVariables addObject:str];
    }
}
[setOfVariables autorelease];
return setOfVariables;
}

When I get to the line

[setOfVariables addObject:str]; 

the app crashes. I've been trying to figure it out for a couple hours, please help! Is there a way in XCode to see the entire list of values in 'anExpression'?

1 Answers1

1

Although you declare your variable as mutable set you create instance of immutable NSSet class - you must create NSMutableSet instance:

NSMutableSet *setOfVariables = [[NSMutableSet alloc] init];
Vladimir
  • 170,431
  • 36
  • 387
  • 313