**`@property(nonatomic, readwrite)** double height;`
무슨 소린지 잘 모르겠으니까 예제를 따라 쳐보며 이해해보도록 하자.
오류 메세지: length라 하는 프로퍼티가 Box 클래스의 객체에서 발견되지 않았다 - 인스턴스 변수 length를 가져올까?
→ = “이 포인터 번지수 가서 이 내용에 접근해라”
다시 뜬 오류 메세지: 프로퍼티가 보호되고 있다..?! 점표기법으로 접근 불가
@private
, @protected
, @public
접근 제한자 예제:
/* 헤더 파일 */
@interface Product : NSObject {
@private
int productId;
@public
NSString *productName;
float price;
}
그렇다고 한다.
수정된 예제:
#import <Foundation/Foundation.h>
@interface Box : NSObject {
double length;
double breadth;
double height;
}
@property(nonatomic, readwrite) double length;
@property(nonatomic, readwrite) double breadth;
@property(nonatomic, readwrite) double height;
-(double)volume;
@end
@implementation Box
@synthesize length;
@synthesize breadth;
@synthesize height;
// initialize
-(id)init { // must include id when initialize (id = pointer of an instance)
self = [super init]; // inherit NSObject
self->length = 1.0;
self->breadth = 1.0;
return self;
}
-(double)volume {
return length * breadth * height;
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
Box *box1 = [[Box alloc] init];
Box *box2 = [[Box alloc] init];
box1.height = 4.0;
box2.height = 34.0;
NSLog(@"Volume of box1 : %f", [box1 volume]); // Volume of box1 : 4.000000
NSLog(@"Volume of box2 : %f", [box2 volume]); // Volume of box2 : 34.000000
}
return 0;
}