2010-11-26 3 views
0

Мое приложение говорит мне, что я чрезмерно отпуская NSDecimalNumber tempDouble ниже:Чрезмерная выпустив NSDecimalNumber

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

+0

На каком языке это? Скажите, пожалуйста, чтобы я мог игнорировать его тег. – 2010-11-26 21:37:17

ответ

0

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

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

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

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

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

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

Смежные вопросы