I make some method, that takes digits and operands from stack and display it in a more user friendly style. The problem is with descriptionString variable, it return null in part when topOfStack is “+”. I show a log below.
+(NSString *)descriptionOfTopOfStack:(NSMutableArray *)stack
{
NSString *descriptionString;
id topOfStack = [stack lastObject];
NSString *secondInStack;
NSString *thirdInStack;
if (topOfStack)
[stack removeLastObject];
if ([topOfStack isKindOfClass:[NSNumber class]]) {
descriptionString = [topOfStack stringValue];
}
else if([topOfStack isKindOfClass:[NSString class]]){
if(([topOfStack isEqualToString:@"+"]) || ([topOfStack isEqualToString:@"—"])){
secondInStack = [self descriptionOfTopOfStack:stack];
thirdInStack = [self descriptionOfTopOfStack:stack];
descriptionString = [descriptionString stringByAppendingFormat:@"%@ %@ %@",thirdInStack,topOfStack,secondInStack];
NSLog(@"description is %@",descriptionString);
}
}
return descriptionString;
}
i made example with 2 + 6, this is log:
- 2012-02-21 22:09:39.983 Calculator[12536:f803] stack = (
2,
6,
“+”
) - 2012-02-21 22:09:39.983 Calculator[12536:f803] description is (null)
Why descriptionString is null? Where i made a mistake? Thanks
In the line:
The variable
descriptionStringisnil. Replace that line with the following.