본문 바로가기

PDA&Mobile

아이폰 앱 개발 팁(3) : Stanford Lecture #3

* 본 포스트는 Blog.MissFlash.com에서 작성한 것으로, 원문 저작자의 동의없이 마음대로 퍼가실 수 없습니다. 포스트의 내용이 마음에 드시면 링크를 이용해주시면 감사하겠습니다.

> Stanford Lecture #3

* Superclass methods
Implicit variable, "self" : Like "this" in Java and C++
Superclass methods, "super"

* Object Creation
1st step : allocate memory to store the object
2nd step : initialize object state

+ alloc : class method(cf. dealloc for destruction)
- init : instance method

Person *person = nil;
person = [[Person alloc] init];

* Multiple init methods
- (id)init;
- (id)initWithName:(NSString *)name;
- (id)initWithName:(NSString *)name age:(int)age;

- (id) init {
return [self initWithName:@"No Name"];
}
- (id) initWithName:(NSString *)name {
return [self initWithName:name age:0];
}

* Reference Counting
As long as retain count is greater than zero, object is alive and valid.
+alloc and -copy create objects with retain count == 1.
-retain increments retain count.
-release decrements retain count.
When retain count reaches 0, object is destroyed.(-dealloc method invoked automatically.)

You only deal with alloc, copy, retain, release.
You never call dealloc explicitly in your code except for "[super dealloc]".

* Returning a newly created object
- (NSString *) fullName {
NSString *result;
result = [[NSString alloc] initWithFormat:@"%@ %@", firstName, lastName];
[result autorelease];
return result;
}
The result is released, but not right away. Caller gets valid object and could retain if needed.

PS. 복잡한 코드와 영어로 뒤덮인 포스트를 맑게 바꿔줄 사진 한 장!

사진 출처 : http://www.flickr.com/photos/stevewall/3632440776/