[英]how to use a NSDecimalNumber object value in a different function?
我是客觀的新手,我想知道如何做到這一點。
我有一個函數,在其中進行一些計算並將結果存儲在NSDecimalNumber對象中。 我想將結果用於其他功能。有人可以幫助我! 謝謝!
例:
- (IBAction)calculate:(id)sender{
NSDecimalNumber *one = [[NSDecimalNumber alloc]initWithString:@"1"];
NSDecimalNumber *two = [[NSDecimalNumber alloc]initWithString:@"2"];
//result is a NSDecimalNumber object created in my .h file
result = [[NSDecimalNumber alloc]init];
result = [one decimalNumberByAdding:two];
//result is now equal to 3
}
- (IBAction)findResult:(id)sender{
NSDecimalNumber *findNumber = [[NSDecimalNumber alloc]init];
//I want "findNumber" to have the same value as result, which was given the value of 3 in method calculate
findNumber = result;
}
如果要像這樣設置變量,則無需先調用alloc / init。 EG,您的第一種方法就是
NSDecimalNumber *one = [[NSDecimalNumber alloc]initWithString:@"1"];
NSDecimalNumber *two = [[NSDecimalNumber alloc]initWithString:@"2"];
result = [one decimalNumberByAdding:two];
假設,也就是說,您之前已經聲明了result,如下所示:
@interface MyClass {
NSDecimalNumber *result;
}
然后,在第二個函數中,如果確實需要結果的副本,而不僅僅是引用結果變量本身,則可以執行以下操作:
NSDecimalNumber *findNumber = [result copy];
完成后記得釋放。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.