2011-04-06 5 views
1

나는 객관적이고 새로운 방법을 알고 싶었다.다른 함수에서 NSDecimalNumber 객체 값을 사용하는 방법은 무엇입니까?

나는 계산을하고 NSDecimalNumber 개체에 결과를 저장하는 함수가 있습니다. 다른 function.can에서 그 결과를 사용하고 싶습니다. 아무도 도와주세요! 감사!

예 :

- (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; 

} 

답변

1

이 같은 변수를 설정하는 경우, 당신은/처음 초기화 ALLOC를 호출 할 필요가 없습니다.

@interface MyClass { 
NSDecimalNumber *result; 
} 

그런 다음, 두 번째 기능에 당신이 정말로 사본이 필요한 경우 : EG, 첫 번째 방법은 당신이 아니라에서 전에 결과를 선언 한 것을

NSDecimalNumber *one = [[NSDecimalNumber alloc]initWithString:@"1"]; 
NSDecimalNumber *two = [[NSDecimalNumber alloc]initWithString:@"2"];  
result = [one decimalNumberByAdding:two]; 

는, 즉 가정 할 수있다 대신 결과 변수 자체를 참조 결과, 당신은 할 수 :

NSDecimalNumber *findNumber = [result copy]; 

이이 완료되면 해제해야합니다.

관련 문제