u 오브젝트의 메모리 할당과 초기화

n         객체생성시 요구사항

동적으로 새로운 객체를 메모리에 할당

새롭게 메모리에 할당된 객체에 적절한 값들로 초기화

작성예

id anObject = [SomeClass alloc];

[anObject init];

[anObject someOtherMessage];

간략화

id anObject = [[SomeClass alloc] init];

[anObject someOtherMessage];

반환값 체크

id anObject = [[SomeClass alloc] init];

if ( anObject )

[anObject someOtherMessage];

else

...

 

n         NSObject alloc, allocWithZone

NSObject는 모든 클래스가 상속받게되는 최상위 클래스 (자바의 Object클래스)

생성한 클래스와 클래스내의 인스턴스 변수들을 위한 메모리 공간 확보

확보한 메모리공간을 생성한 인스턴스변수가 가르키도록 지시(Pointer)

모든 인스턴스 변수들의 값을 0으로 세팅

 

n         초기화 메소드 구현

초기화 인스턴스 메소드의 명칭은 init로 시작

인자가 없는 초기화 메소드는 init:

인자가 있는 초기화 메소드는 initMethodName:

 

n         초기화 메소드 작성예

- 인자가 없는 경우

- (id)init {

self = [super init];

if (self) {

creationDate = [[NSDate alloc] init];

}

return self;

}

 

 

- 인자가 있는 경우

 

- (id)initWithImage:(NSImage *)anImage {

NSSize size = anImage.size;

NSRect frame = NSMakeRect(0.0, 0.0, size.width, size.height);

self = [super initWithFrame:frame];

If (self) {

image = [anImage retain];

}

return self;

}

 

n         메모리확보와 초기화의 결합

convenience constructors를 사용함으로서 메모리 자동해제(autorelease)를 적용

자동해제의 경우 직접해제를 할경우 크래시 발생

Posted by 김반장78
,

 

u 클래스 정의

n         Interface : 한 클래스의 메소드와 인스턴스 변수를 선언하고, 어느 수퍼클래스로부터 상속을 받는지를 기입

헤더 파일인 *.h 파일에 정의

 

n         Implementation : 실지로 클래스를 정의하는 부분

구현 파일인 *.m에 구현

 

n         해더 파일과 구현부를 나누는 이유

외부에서 볼때 구현부를 독립해 접근 불가능

 

n         작성 요령

가능하면 1:1로 작성 이름을 통일

 

n         인터페이스 작성 예

@interface ClassName : ItsSuperClass

{

instance variables declaration..

}

method declarations

@end

 

n         메소드 선언시 옵션

-

Instance method

+

Class method

              메소드 선언시 인스턴스와 클래스메소드 같은 이름으로 생성 가능

              메소드 이름과 인스턴스 변수도 같은 이름으로 생성 가능

 

n         메소드 선언 예

-선언

- (void) setWidth: (float) width height: (float) height;

-사용

id *myRect = [[Rectangle alloc] init];

[myRect setWidth:12.2 height:18.0];

-가변적 변수수를 가진 메소드 선언

- makeGroup:group, …;

 

n         interface import

interface import C #include와 동일하나, import가 똑 같은 것을 한번만 가져오는 것만 inclue와 다름.

 

n         implementation 작성예

@implementation ClassName : ItsSuperclass -> superclass 생략 가능

{

    instance variable declarations -> 생략 가능

}

method definitions

@end

              간단히 쓰면,

#import "ClassName.h"

 @implementation ClassName

method definitions

@end

 

n         다른 클래스 참조

인터페이스에 선언되지 않은 클래스를 사용할 때

상호참조의 문제를 해결하기 위해 @class 지시자를 사용.

@class Rectangle, Circle;

이것은 단지 컴파일러에 이것이 클래스 이름이라는 것만 알려주는 역할

 

n         인터페이스 특징

인터페이스파일은 클래스의 참조현황을 파악하기 쉽게함

인스턴스 개체를 따로 관리함으로서 보다 직관적인 OOP를 가능하게 함

 

n         instance변수들의 사용범위를 설명한 지시자

@private

instance변수의 범위를 그것이 선언된 class 한정한다.(상속불가)

@protected

instance변수의 범위를 선언되고, 상속된 class 한정한다. (상속가능)

@public

instance변수의 범위에 대한 제한을 없앤다. (상속가능)

 

n         instance변수 범위 지정의 이유

슈퍼클래스 변수와의 독립성을 유지함으로서 보다 완벽한 오브젝트화 가능

 

n         다른 객체에 속한 인스턴스 변수사용 예

선언구현부

@interface Sibling : NSObject

{

Sibling *twin;

int gender;

struct features *appearance;

}

참조구현부

- makeIdentification

{

if( !twin)

{

twin = [[Sibling alloc] init];

twin->gender = gender;

twin->appearance = appearance;

}

return twin;

}

 

 

영문 레퍼런스트를 정리하다가 갑자기 생겨난 쓸데없는 오기..ㅡ.ㅡ
도움 되려나~ 


Posted by 김반장78
,