프로그래밍/Objective C 13

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..

Objective C - @class 지시어 사용하기

@class지시어를 사용하면 그 클래스 형식의 인스턴스 변수를 만나게 될 때 컴파일러에게 그 클래스가 무엇인지 알려준다. 이것대신 헤더파일을 import해도 상관 없다. Rectangle.h #import @classXYPoint;//이러한 클래스가 쓰인다는 것만 알려줌 대신 import로 헤더파일을 불러와도 됨 @interface Rectangle: NSObject @property int width, height; -(XYPoint *) origin;//추가됨 -(void) setOrigin: (XYPoint *) pt; -(int) area; -(int) perimeter; -(void) setWidth: (int) w andHeight: (int) h; @end Rectangle.m #import..

Objective C - 상속으로 확장하기 (새 메소드 추가)

상속을 이해하기 위한 간단한 예제를 만들어 봅시다. 먼저 Rectangle클래스는 NSObject 클래스를 상속받고 Square클래스는 Rectangle클래스를 상속받습니다. 그리고 Rectangle클래스에서는 멤버변수들을 @property 지정을 해서 자동으로 게터와 세터 메소드를 생성시킵니다. 그런뒤 Square클래스에서는 self.width 를 통해 부모클래스의 width값을 접근할 수 있습니다.(게터 메소드에 의해서) Rectangle.h #import @interface Rectangle: NSObject @property int width, height; -(int) area; -(int) perimeter; -(void) setWidth: (int) w andHeight: (int) h; @..

Objective C - 간단한 예제를 이용한 상속이해

기본적으로 모든 클래스는 NSObject를 상속받는다. 이것만 유의하고 다음 예제를 보면 쉽게 상속을 이해할 수 있다. #import //ClassA 선언과 정의 @interface ClassA: NSObject{ int x; } -(void) initVar; @end @implementation ClassA -(void) initVar{ x = 100; } @end //ClassB 선언과 정의 ClassA로부터 상속 받음 @interface ClassB : ClassA -(void) printVar;//ClassB만의 고유 함수 @end @implementation ClassB -(void) printVar{ NSLog(@"x = %i", x); } @end int main(int argc, const..

Objective C - 세터(setter)와 게터(getter) 자동 생성 및 클래스를 인자로 넘기기

Fraction.h 파일에서 멤버 변수에 @property를 지정해주면 자동으로 세터 함수와 게터 함수가 생성된다. Fraction.m파일에서는 @synthesize 를 멤버변수에 해줘야함.. Fraction.h #import @interface Fraction : NSObject //아래와 같이 하면 자동으로 세터(setter)와 게터(getter)가 생성된다. @property int numerator, denominator; -(void) print; -(void) setTo: (int) n over: (int) d; -(double) convertToNum; -(void) add: (Fraction *) f; -(void) reduce; @end Fraction.m #import "Fraction..

Objective C - 인터페이스와 구현파일 나누기

위와 같이 Fraction 클래스부분과 메인 부분을 나눈다. 다음은 인터페이스부분인 Fraction.h 이다. #import //Fraction 클래스 @interface Fraction : NSObject -(void) print; -(void) setNumerator: (int) n; -(void) setDenominator: (int) d; -(int) numerator; -(int) denominator; -(double) convertToNum; @end 다음은 구현부분인 Fraction.m이다. #import "Fraction.h" @implementation Fraction{ int numerator; int denominator; } -(void) print{ NSLog(@"%i%i", n..

반응형