티스토리 뷰
Getter & Setter 를 사용하여 멤버변수 생성하기
@interface Employee : NSObject
{
int employeeNumber;
NSString *name;
}
- (void) setName : (NSString *) newName;
- (NSString *) name; @end
Accessor 를 사용하여 멤버변수 생성하기
@interface Employee : NSObject
{
int employeeNumber;
NSString *name;
}
@property (nonatomic, assign) int employeeNumber;
@property (nonatomic, retain) NSString *name;
@end
@implementation Employee
@synthesize employeeNumber;
@synthesize name;
@end
프로퍼티 이름 바구기
@interface Employee : NSObject
{
int employeeNumber;
NSString *name;
}
@property (nonatomic, assign) int employeeNumber;
@property (nonatomic, retain) NSString *userName;
@end
@implementation Employee
@synthesize employeeNumber;
@synthesize userName = name;
@end
Assign: 비 오브젝트 프로퍼티로 사용할 수 있는 단 하나의 속성이다.
Retain: retain속성을 사용하고 @synthesize를 사용하면 컴파일러는 레퍼런스 카운팅을 맞춰주는 작업을 하기위한 setter를 생성한다.
Copy: setter에서 오브젝트를 복사하여 주소를 넘긴다.
만약 위 세가지를 명시하지 않는다면 컴파일러는 기본값인 assign으로 인식한다.
@dynamic: 만약 accessor메소드를 직접 작성한다면 컴파일러는 메소드가 @property 선언문에서 지정한 속성과 일치하는지 검사하지 않고 (copy 혹은 readonly) 로 선언해도 setter메서드를 작성성하는 동안 그냥 묵인한다.
nonatomic: 컴파일러는 스레드 보호에 대한 고려없이 accessor를 synthesize한다.
readyonly를 위한 숨겨진 setter를 사용하려면 @implementation 위 즉 파일 상단에 아래와 같이 작성하면 된다.
@interface Employee ()
@property (nonatomic, readwrite) int employeeNumber;
@end
@implementation Employee
...
@end
'IOS' 카테고리의 다른 글
레이아웃 - 스크롤뷰의 컨텐츠 추가 삭제 하기 (0) | 2018.08.28 |
---|---|
IOS 스레드 제어 (0) | 2018.08.28 |
PDF 파일 생성 및 저장(UIView를 이용) (1) | 2018.03.29 |
클래스 오브젝트 초기화 (0) | 2017.03.29 |
@Property, @Synthesize, @Dynamic (0) | 2017.03.29 |