Objective-C属性 - getter行为

Dan*_*iel 5 getter properties objective-c

以下是技术上的错误:

@property(nonatomic, assign) NSUInteger timestamp;
@property(nonatomic, readonly, getter = timestamp) NSUInteger startTime;
@property(nonatomic, assign) NSUInteger endTime;
Run Code Online (Sandbox Code Playgroud)

我确信我可以找到一种更好的方法来组织它,但这是我在项目中的某个时刻得到的结果,我注意到访问startTime属性总是返回0,即使timestamp属性设置为正确的时间戳.

似乎已将startTime的getter设置为现有属性(timestamp),当我这样做时,它不会转发timestamp的值:

event.startTime => 0
event.timestamp => 1340920893
Run Code Online (Sandbox Code Playgroud)

所有这些都是时间戳.

提醒一下,我知道上面应该发生在我的项目中,但我不明白为什么访问startTime不会转发到timestamp属性.

UPDATE

在我的实现中,我正在综合所有这些属性:

@synthesize timestamp, endTime, startTime;
Run Code Online (Sandbox Code Playgroud)

请检查一个示例对象,以便在GitHub的gist上演示这个:https://gist.github.com/3013951

zpa*_*ack 7

在您的描述方法中,您没有使用该属性,您正在访问ivar.

-(NSString*) description
{
    return [NSString stringWithFormat:@"Event< timestamp:%d, start:%d >", 
             timestamp, 
             startTime]; // <-- This is accessing the instance variable, not the property.
}
Run Code Online (Sandbox Code Playgroud)

这对你有用:

-(NSString*) description
{
    return [NSString stringWithFormat:@"Event< timestamp:%d, start:%d >", 
             timestamp, 
             self.startTime]; // <-- This is using the property accessor.
}
Run Code Online (Sandbox Code Playgroud)

属性与伊达之类的东西一直让人感到困惑,所以请原谅我,一会儿我絮絮叨叨.:)如果您已经知道所有这些,请跳过.

当您创建和合成属性时,如上所述,会发生两件事:

  1. ivar是由适当的类型创建的.
  2. 创建一个getter函数,返回该值.

关于第2点的重要部分是,默认情况下,ivar和getter函数(因此属性)具有相同的名称.

所以这:

@interface Event
@property(nonatomic, assign) NSUInteger timestamp;
@property(nonatomic, readonly, getter = timestamp) NSUInteger startTime;
@end

@implementation Event
@synthesize timestamp, startTime;
@end
Run Code Online (Sandbox Code Playgroud)

......变成这个:

@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
Run Code Online (Sandbox Code Playgroud)

点语法的工作原理是:

NSUInteger foo = myEvent.startTime;
Run Code Online (Sandbox Code Playgroud)

真的

NSUInteger foo = [myEvent startTime];
Run Code Online (Sandbox Code Playgroud)

所有这一切都说,当你访问一个伊娃,你是......好吧,访问一个伊娃.当您使用属性时,您正在调用一个返回值的函数.更重要的是,当你指的是另一个时,做一件事非常容易,因为语法非常相似.正是由于这个原因,许多人经常将他们的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.  :)
Run Code Online (Sandbox Code Playgroud)