설명 방법에서 귀하는 ivar에 액세스하는 속성을 사용하고 있지 않습니다.
-(NSString*) description
{
return [NSString stringWithFormat:@"Event< timestamp:%d, start:%d >",
timestamp,
self.startTime]; // <-- This is using the property accessor.
}
속성 - 대 - 바르 것은 항상 사람을 망쳐 놨, 그래서 잠시 그것에 대해에 산책하면서 실례 :
-(NSString*) description
{
return [NSString stringWithFormat:@"Event< timestamp:%d, start:%d >",
timestamp,
startTime]; // <-- This is accessing the instance variable, not the property.
}
이 당신을 위해 작동합니다. :) 이미이 모든 것을 알고 있다면 앞으로 건너 뜁니다. 위처럼
만들고 속성을 합성
는 두 가지 일이 :
- 바르가의 유형이 생성됩니다.
- getter 함수가 생성되어 해당 ivar이 반환됩니다. 포인트 약 2
중요한 부분은 기본적 바르 게터 함수 (따라서, 속성)는 동일한 이름을 가지고 있다는 것이다.
그래서이 :
@interface Event
@property(nonatomic, assign) NSUInteger timestamp;
@property(nonatomic, readonly, getter = timestamp) NSUInteger startTime;
@end
@implementation Event
@synthesize timestamp, startTime;
@end
...이로 바뀝니다 :
NSUInteger foo = myEvent.startTime;
정말 당신이 바르에 액세스 할 때, 당신이있어 말을
NSUInteger foo = [myEvent startTime];
그 모든 작업을 수행합니다
@interface Event {
NSUInteger timestamp;
NSUInteger startTime;
}
@end
@implementation Event
- (NSUInteger) timestamp {
return timestamp
}
- (void) setTimestamp:(NSUInteger) ts {
timestamp = ts;
}
- (NSUInteger) startTime {
return [self timestamp];
}
@end
어떻게 도트 구문 작동이가 있다는 것입니다 ... 잘, ivar에 접근했다. 속성을 사용할 때 값을 반환하는 함수를 호출하고 있습니다. 더 중요한 것은 구문이 매우 유사하기 때문에 다른 것을 의미 할 때 한 가지 일을하는 것이 매우 쉽습니다. 그것은 많은 사람들이 일상적으로 선도적 인 밑줄로 그들의 ivars를 종합하므로, 엉망이되기 어렵습니다.
@property(nonatomic, assign) NSUInteger timestamp;
@property(nonatomic, readonly, getter = timestamp) NSUInteger startTime;
@synthesize timestamp = _timestamp;
@synthesize startTime = _startTime;
NSLog(@"startTime = %d", _startTime); // OK, accessing the ivar.
NSLog(@"startTime = %d", self.startTime); // OK, using the property.
NSLog(@"startTime = %d", startTime); // NO, that'll cause a compile error, and
// you'll say "whoops", and then change it
// to one of the above, thereby avoiding
// potentially hours of head-scratching. :)
무엇이 오류입니까? 'getter '로? –
사실 내 대답이 잘못되었습니다. 동일한 선택자에 여러 속성이 연결되어있는 경우 중요하지 않습니다. 나는이 문제에 아무런 문제가 없다고 말할 것이다 ... –
@ RichardJ.RossIII 나는 ARC 프로젝트의 최신 XCode에서 나의 테스트를 수행했다. 이것이 어떤 시점에서 Clang의 변화 일 수도 있습니다. – Joe