Превышение NSDecimalNumber - PullRequest
       3

Превышение NSDecimalNumber

0 голосов
/ 27 ноября 2010

Мое приложение сообщает, что я переиздаю NSDecimalNumber tempDouble ниже:

NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init];
        [currencyFormatter setLocale:[NSLocale currentLocale]];
        [currencyFormatter setGeneratesDecimalNumbers:TRUE];
        [currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];

        // Here is the key: use the maximum fractional digits of the currency as the scale
        int currencyScale = [currencyFormatter maximumFractionDigits];

        NSDecimalNumber* tempDouble = [[NSDecimalNumber alloc] initWithString:self.tempStore];


        NSDecimalNumber* numTen = [[NSDecimalNumber alloc] initWithInt:10];

        tempDouble = [tempDouble decimalNumberByDividingBy:[numTen decimalNumberByRaisingToPower:currencyScale]];

        [numTen release];


        [textField setText:[currencyFormatter stringFromNumber:tempDouble]];

        [currencyFormatter release];
        [tempDouble release];

Я думаю, что проблема в следующем:

tempDouble = [tempDouble decimalNumberByDividingBy:[numTen decimalNumberByRaisingToPower:currencyScale]];

Но я не уверен почему. Должен ли я добавить атрибут «назначить, скопировать или сохранить» после назначения? Когда я избавляюсь от выражения 'release' ниже, код работает нормально.

Спасибо

G

1 Ответ

0 голосов
/ 27 ноября 2010

Проблема действительно в этой строке:

tempDouble = [tempDouble decimalNumberByDividingBy:[numTen decimalNumberByRaisingToPower:currencyScale]];

То, что вы здесь делаете, это замена предыдущего значения в tempDouble (которое имеет счет сохранения 1) новым значением (которое автоматически высвобождается).).

Вот правильный способ сделать это:

NSDecimalNumber* tempDouble = [[[NSDecimalNumber alloc] initWithString:self.tempStore] autorelease];

И затем удалить [tempDouble release] в конце метода.

...