본문 바로가기

PDA&Mobile

아이폰 앱 개발 팁(12) : Head First iPhone Development #3

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

> Head First iPhone Development 정리 #3

Chapter 3. objective-c for the iPhone - Twitter needs variety

@property(in the .h file) & @synthesize(in the .m file) : tell Objective-C to autogenerate getter and setter methods.

-(The minus sign) : it's an instance method.
+(The plus sign) : it's an class method(at means it's static).

Property attributes and definitions
  • readonly : When you don't want people modifying the property. You can still change the field value backing the property, but the compiler won't generate a setter.
  • readwrite : When you want the property to be modifiable by people. The compiler will generate a getter and a setter for you. This is the default.
  • copy : When you want to hold onto a copy of some value instead of the value itself; for example, if you want to hold onto an array and don't want people to be able to change its contents after they set it. This sends a copy message to the value passed in then retains that.
  • retain : When you're dealing with object values. The compiler will retain the value you pass in and release the old value when a new one comes in.
  • assign : When you're dealing with basic types, like ints, floats, etc. The compiler just creates a setter with a simple myField = value statement. This is the default, but not usually what you want.
  • atomic : Locks, Generated accessors are multithread safe and use mutexes when changing a property value. (cf. nonatomic : unlocks)

Example
  • retain : @ property (nonatomic, retain) NSString* myField;
- (NSString*) myField{
    return myField;
}
- (void)setMyField:(NSString *) newValue{
    if(newValue != myField){
        [myField release];
        myField = [newValue retain];
    }
}

  • assign : @ property (nonatomic, assign) NSString* myField; (Unproper to the NSString!!!)
- (NSString*) myField{
    return myField;
}
- (void)setMyField:(NSString *) newValue{
    myField = newValue;
}

To keep your memory straight!!!
  1. You must release objects you create with allocnewcopy, or mutableCopy.
  2. Consider everything else to have a retain count of 1 and in the autorelease pool.

TextField Done Editing

- (IBAction)textFieldDoneEditing: (id)sender

{

    [sender resignFirstResponder]; 

}


이미지 출처 : flickr.com