我有以下属性:
@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。
答案 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];
}
}