O bjective C 3

Objective C - 카테고리의 이해

카테고리에 대한 전반적 설명 : http://soooprmx.com/wp/archives/2436 카테고리는 한 클래스를 확장할때 자식 클래스를 생성하지않고도 확장할수있는 방법이다. 다음과 같이 Fraction 클래스가 있을 때 add, mul, sub, div 메소드를 더 추가하고 싶다면 다음과 같이 하면된다. 아래 예제를 보면 앞쪽에 Fraction 클래스를 명시하고 (카테고리명)을 추가시켜주면 된다. #import #import "Fraction.h" @interface Fraction (MathOps) -(Fraction *)add:(Fraction *)f; -(Fraction *)mul:(Fraction *)f; -(Fraction *)sub:(Fraction *)f; -(Fraction *)d..

Objective C - 동적바인딩과 id형

위와 같이 프로젝트를 구성한우 아래 소스를 입력한다. Fraction.h #import @interface Fraction : NSObject //아래와 같이 하면 자동으로 세터(setter)와 게터(getter)가 생성된다. @property int numerator, denominator; -(void) print; -(void) setTo: (int) n over: (int) d; -(double) convertToNum; -(Fraction *) add: (Fraction *) f; -(void) reduce; @end Fraction.m #import "Fraction.h" @implementation Fraction //세터(setter)와 게터(getter) 생성시 반드시 아래와 같이 해줘야..

Objective C - 메소드 재정의하기

부모 메소드를 자식에서 재정의 할 수 있다. #import //Class A 선언 및 정의 @interface ClassA: NSObject{ int x; } -(void) initVar; @end @implementation ClassA -(void) initVar{ x = 100; } @end //Class B 선언 및 정의 @interface ClassB: ClassA -(void) initVar; -(void) printVar; @end @implementation ClassB -(void) initVar{ x = 200; } -(void) printVar{ NSLog(@"x = %i", x); } @end int main(int argc, const char * argv[]) { @autorel..

반응형