自定义setter for double

时间:2012-03-28 05:51:27

标签: iphone objective-c ios ipad core-data

我有以下属性:

@property (nonatomic, assign) double lastSynced;

and here's my custom setter:

-(void)setLastSynced:(double)newLastSynced
{
    if (lastSynced != newLastSynced){
        lastSynced = newLastSynced;
        [[NSUserDefaults standardUserDefaults] setDouble:lastSynced forKey:kSettingLastSyncedKey];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

我不确定为什么在设置lastSynced时我的自定义setter没有被调用?我把它合成为

  

@synthesized lastSynced。

2 个答案:

答案 0 :(得分:0)

您是否尝试过明确声明您的二传手?

@property (nonatomic, assign, setter=setLastSynched:) double lastSynced;

答案 1 :(得分:-1)

非常糟糕的是,在你创建自定义setter的过程中你没有写lastSynced = _lastSynced是非常糟糕的,因为在setter中你试图调用你的setter。 尝试:

@synthesize lastSynced = _lastSynced

-(void)setLastSynced:(double)newLastSynced
{
         if (_lastSynced != newLastSynced)
    {
            _lastSynced = newLastSynced;
            [[NSUserDefaults standardUserDefaults] setDouble:_lastSynced  forKey:kSettingLastSyncedKey];
            [[NSUserDefaults standardUserDefaults] synchronize];
    }
}