티스토리 뷰
나름 프로그래밍?/ Objective-C
3. Custom Classes, Object Lifecycle, Autorelease, Objective-C Properties (January 12, 2010)
-Dong- 2010. 4. 8. 08:34
어떻게 알아 듣지도 못하는 영어로 강의하는 Stanford 자료(Itune U에 무료로 올라가 있음)가
공부하는데 도움이 더 되는듯 ㅠ_ㅠ
+ alloc
■ Class method that knows how much memory is needed
(alloc 은 메모리 할당 과정)
- init
■ Instance method to set initial values, perform other setup
Obj-C에선 dealloc 을 직접 호출 하는 일이 없음(한가지 예외가 존재하긴 함.. 아래 코드)
- (void)dealloc
{
// Do any cleanup that’s necessary
// ...
// when we’re done, call super to clean us up
[super dealloc];
} @end
+alloc and -copy create objects with retain count == 1
(copy 도 retain 카운트에 영향)
Person *person = [[Person alloc] init];
// ...
[person release]; // Object is deallocated
person = nil;
[person doSomething]; // No effect
(retain count가 0이 되어 자동으로 dealloc 호출되고 메모리 해제된 뒤 호출시 에러 발생. nil 을 넣어주면 괜찮음)
Autoreleasing Objects
• Calling -autorelease flags an object to be sent release at some point in the future
• Let’s you fulfill your retain/release obligations while allowing an object some additional time to live
• Makes it much more convenient to manage memory
• Very useful in methods which return a newly created object
- (NSString *)fullName {
NSString *result;
result = [[NSString alloc] initWithFormat:@“%@ %@”, firstName, lastName];
[result autorelease];
return result;
}
(Autoreleasing 은 위와 같이 return 해줄때 유용, return 전에 release 하면 너무 빨리 해제되어 버리고 return 시에는 leak 발생)
How does -autorelease work?
• Object is added to current autorelease pool
• Autorelease pools track objects scheduled to be released
■ When the pool itself is released, it sends -release to all its objects
• UIKit automatically wraps a pool around every event dispatch
(autorelease 는 Pool 을 하나 생성하고 autorelease 가 호출된 객체들을 담고 있다가 Pool 이 release 될때 한번에 release)
Method Names & Autorelease
• Methods whose names includes alloc, copy, or new return a retained object that the caller needs to release
NSMutableString *string = [[NSMutableString alloc] init];
// We are responsible for calling -release or -autorelease
[string autorelease];
• All other methods return autoreleased objects
NSMutableString *string = [NSMutableString string];
// The method name doesn’t indicate that we need to release it
// So don’t- we’re cool!
• This is a convention- follow it in methods you define!
(Naming convenitino 을 이용해 autorelease 된 객체를 전달해 주니 편하게 쓰라곤 하는데 책에선 alloc, init 으로 가자는걸 본거 같은데.. 말이 틀려 ㅠ_ㅠ)
(NSString 은 string NSArray 는 array, stringWithFormat, stringWithString 등등 alloc, init의 짝은 initWithFormat, initWithString 등등)
Property Attributes
• Read-only versus read-write
@property int age; // read-write by default
@property (readonly) BOOL canLegallyVote;
• Memory management policies (only for object properties)
@property (assign) NSString *name; // pointer assignment
@property (retain) NSString *name; // retain called
@property (copy) NSString *name; // copy called
>property 예
// property declarations
@property int age;
@property (copy) NSString *name;
@property (readonly) BOOL canLegallyVote;
@implementation Person
@synthesize age;
@synthesize name;
- (BOOL)canLegallyVote
{
return (age > 17);
}
@end
>필요에 의해 setter, getter 를 property 가 명시되어도 작성 가능
출저 -
ITunes U : iPhone Application Development (Winter 2010) - Tracks
'나름 프로그래밍? > Objective-C' 카테고리의 다른 글
5. Views, Drawing, and Animation (January 19, 2010) (0) | 2010.04.09 |
---|---|
4. Building an Application; Model, View, Controller; Nib Files; Controls and Target-Action (January 14, 2010) (0) | 2010.04.09 |
XCode 에서 생긴 My Error Solution (1) | 2010.03.16 |
경고창 띄우기 (0) | 2010.03.02 |
액션 시트 띄우기 (0) | 2010.03.02 |