English 中文(简体)
NSNumber储存零价值,而不是正确的数字价值
原标题:NSNumber stores zero value instead of the correct numeric value

我开发了一个简单的计算器应用程序。 就实际目的而言。 我采用一种“国际行动”方法,储存用户输入的数位数。 整个概念是,计算器在投影之前对碎块进行过滤,以便用户能够采取多种行动,屏幕显示其结果如下:1+ 2 - 4 + 10 = X. 因此,即有2个NSMutableArray,储存NSNumber号和操作者行动。 当用户点击运营商纽顿时,为新数字创造了一个新的阵列。 当用户进入数字时,最后一个阵列元素正在更新,直到操作者纽顿受到压力。

The problem is that every array element is zero. Inside the method it stores the corrent value when i set it, but when the method is executed and called again it contins nothing but zeros instead of the entered numbers. The NSNumber objects are present, the array contains every number, but every number is 0.

这里的方法是:

// Processing digit buttons 
- (IBAction)clickDigit: (UIButton *) sender {

    double currentNumber = [[numbers lastObject] doubleValue];

    // Get the digit
    double digit = sender.tag;

    // Do nothing when the currentNumber and the digit both zero
    if(currentNumber == 0 && digit == 0) {
        return;
    }

    // When currentNumber is zero, override the value
    if(currentNumber == 0) {

        currentNumber = digit;

        [numbers removeLastObject];
        [numbers addObject: [NSNumber numberWithDouble: currentNumber ]];

    // Else, add the digit to currentNumber
    } else {

        currentNumber = currentNumber * 10 + digit;

        [numbers removeLastObject];
        [numbers addObject: [NSNumber numberWithDouble: currentNumber ]];
    }

    // Update the screen
    [self updateDisplay];
}

I have no clue what s wrong. Any suggestions? Thanks in advance

<>UPDATE:指点击 每顿报后自动使用明确的方法。 该数值为零。 我在评论部分将全部来源代码与这一员额联系起来。 唯一的问题是:为什么使用这种方法? 所谓的这种方法是什么?

UPDATE2: with The Saad s help i managed to solve this problem. Thanks to everyone! :)

问题回答

回答这一问题的唯一可能途径是sender.tag = 0。 因此,你应明确检查这一点。 我在此指出,没有其他可能的设想能够产生这些症状,因此必须这样做。

ok got it,

这里有一件事是,首先用布拉克点或国家安全记录仪对你的ender子进行点击。 血清方法,即gues子永远不会达到零,因此,阵列中最后的物体将永远是零,进一步,在你增加阵列反对的所有地点放置ns或断点。

in interface.

@property(nonatomic, retain) NSMutableArray* numberArray;

in implementation,

@synthesize numberArray = _numberArray;

in dealloc

[_numberArray release];

下面是做的事情,是制造一阵 in,即:

NSMutableArray* arr = [NSMutableArray alloc] init];
self.numberArray = arr;
[arr release];

[self.numberArray addObject:[NSNumber numberWithDouble:0]];




相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签