Превышение NSDecimalNumber

Мое приложение сообщает мне, что я переиздаю 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, код работает нормально.

Спасибо,

г

1 ответ

Решение

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

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

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

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

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

А затем удалите [tempDouble release] с конца метода.

Другие вопросы по тегам