2010-08-02 5 views

답변

2

다른 클래스에서 액세스하려는 인스턴스 변수 int integerOne이있는 ClassOne 클래스가 있다고 가정하겠습니다. ClassTwo. 이를 처리하는 가장 좋은 방법은 ClassOne에 속성을 만드는 것입니다. ClassOne.h에서 :이 속성을 선언

@property (assign) int integerOne; 

(기본적으로 두 가지 방법, - (int)integerOne- (void)setIntegerOne:(int)newInteger). 그런 다음 ClassOne.m에서 :

이 두 가지 방법을 "합성"합니다. 이것은 기본적으로 동일하다 :이 시점에서

- (int)integerOne 
{ 
    return integerOne; 
} 

- (void)setIntegerOne:(int)newInteger 
{ 
    integerOne = newInteger; 
} 

, 당신은 지금 ClassTwo에서 이러한 메서드를 호출 할 수 있습니다. ClassTwo.m에서 :

#import "ClassOne.h" 
//Importing ClassOne.h will tell the compiler about the methods you declared, preventing warnings at compilation 

- (void)someMethodRequiringTheInteger 
{ 
    //First, we'll create an example ClassOne instance 
    ClassOne* exampleObject = [[ClassOne alloc] init]; 

    //Now, using our newly written property, we can access integerOne. 
    NSLog(@"Here's integerOne: %i",[exampleObject integerOne]); 

    //We can even change it. 
    [exampleObject setIntegerOne:5]; 
    NSLog(@"Here's our changed value: %i",[exampleObject integerOne]); 
} 

이러한 Objective-C 개념을 배우려면 몇 가지 자습서를 읽어야합니다. 나는 these을 제안한다.

관련 문제