- (id) retain; /* retainCount++, return self.
* Used when you want to prevent it from being deallocated
* without your express permission. */
- (void) release; /* retainCount-- */
- (unsigned) retainCount; /* return retainCount */
- (id) autorelease;
- (NSString*) description
{
NSString* str = [[NSString alloc] initWithFormat: @"I am %d years old", 4];
return str;
}
위의 description 메소드는 NSString 객체를 alloc으로 생성하고 그 값을 초기화한 뒤 리턴하고 있다. 리턴된 str 객체는 프로그램의 다른 부분에서 사용될 것이다. 그렇다면 이 str을 사용한 뒤 release는 언제 어디에서 해주어야 하는가. 이러한 문제는 str 객체에 대하여 autorelease 선언을 해줌으로써 해결할 수 있다. 즉 위 코드의 마지막 줄을
return [str autorelease];
NSMutableArray* array = [[NSMutableArray alloc] init];
/* use the array */
[array release];
NSMutableArray* array = [NSMutableArray arrayWithCapacity: 17];
/* use the array */
- (void) doStuff
{
// array is an instance variable
array = [NSMutableArray new];
}
- (void) dealloc
{
[array release];
[super release];
}
- (void) doStuff
{
// array is an instance variable
array = [NSMutableArray arrayWithCapacity: 17];
[array retain];
}
- (void) dealloc
{
[array release];
[super release];
}
'Object-C' 카테고리의 다른 글
textbox 글자수에 따른 height 가져오기 (0) | 2010.11.26 |
---|---|
HIG (0) | 2010.11.07 |
StanFord Lecture PDF (0) | 2010.10.29 |
테이블뷰 정렬 (0) | 2010.10.23 |
키보드 사이즈 이벤트 (0) | 2010.10.23 |