NSMutable 数组 - “不是 Objective-C 对象"错误 [英] NSMutable array - "Not an objective-C object" error

查看:48
本文介绍了NSMutable 数组 - “不是 Objective-C 对象"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不完全确定为什么它在注释行返回错误不是目标 C 对象".任何帮助将不胜感激.

I am not totally sure why it is that it is returning the error "not an objective-C object" at the commented line. Any help would be appreciated.

此外,我对 Objective-C 非常陌生,我确实意识到这很可能是一个非常愚蠢的错误.但是,任何建议都会有所帮助.

Additionally, I am very new to objective-C, and I do realize there is a high possibility this is a very silly mistake. However any advice will help.

#import "CalculatorBrain.h"

@interface CalculatorBrain()
@property (nonatomic, strong) NSMutableArray *operandStack;
@end

@implementation CalculatorBrain

@synthesize operandStack = _operandStack;

- (NSMutableArray *)operandStack
{
    if(!_operandStack){
    _operandStack = [[NSMutableArray alloc] init];
    }// end if
    return _operandStack;
}//end operandStack

- (void)pushOperand:(double)operand
{
    NSNumber *operandObject = [NSNumber numberWithDouble:operand];
    [self.operandStack addObject:operandObject];
}//end pushOperand

- (double)popOperand
{
    NSNumber *operandObject = [self.operandStack lastObject];//error "Not an objective-c object"
    if(operandObject) [self.operandStack removeLastObject];
    return [operandObject doubleValue];
}//end popOperand

- (double)performOperation:(NSString *)operation
{
    double result = 0;

    if([operation isEqualToString:@"+"]){
    result = [self popOperand] + [self popOperand];
} else if([operation isEqualToString:@"-"]){
    double subtrahend = [self popOperand];
    result = [self popOperand] - subtrahend;
} else if([operation isEqualToString:@"*"]){
    result = [self popOperand] * [self popOperand];
} else if([operation isEqualToString:@"/"]){
    double divisor = [self popOperand];
    if(divisor) result = [self popOperand] / divisor;
}//end if
[self pushOperand:result];
return result;

}//performOperation

@end

推荐答案

我认为这可能是由于函数 popOperand 中的操作顺序不正确造成的.看我的内联评论,最后一行 return [operandObject doubleValue] 访问了一个已经发布的对象.多次运行此代码后,可能会导致内存问题,反过来,您可能会在注释行看到错误.

I think this might be caused by the the incorrect operation order in the function popOperand. See my comments inline, the last line return [operandObject doubleValue] accessed a already released object. After running this code several time, it might cause memory problem and in turn, you might see the error at your comment line.

- (double)popOperand
{
    NSNumber *operandObject = [self.operandStack lastObject];
    if(operandObject) [self.operandStack removeLastObject];
    // When you remove the lastObject(operandObject) from the Array, the operandObject retainCount is zero.
    // Here, the operandObject is deallocated and should not be used.
    // But you call it after it was released.
    return [operandObject doubleValue];
}//end popOperand

这可能没问题,当您只调用一次或多次 popOperand 时.但这会导致内存错误.我想每次调用 popOperand 时都不会发生错误.这是我的解决方案.

This might be OK, when you only call popOperand once or several times. But this will result memory error. I guess the error doesn't occur every time when the popOperand is called. Here is my solution.

- (double)popOperand
{
    double result = 0.0;
    NSNumber *operandObject = [self.operandStack lastObject];
    if(operandObject) 
    {
        result = [operandObject doubleValue];
        [self.operandStack removeLastObject];
    }
    return result;
}

这篇关于NSMutable 数组 - “不是 Objective-C 对象"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆