综合技术

Getter inside setter in Swift 3

微信扫一扫,分享到朋友圈

Getter inside setter in Swift 3
0

I’m using this variable

public var tick: Int64 {
    get {
        return UserDefaults.standard.object(forKey: TICK) as? Int64 ?? 0
    }
    set(v) {
        let cur = UserDefaults.standard.object(forKey: TICK) as? Int64 ?? 0 + v
        UserDefaults.standard.set(cur, forKey: TICK)
    }
}

But I want to know, that is the proper way to call getter in inside setter? I mean:

set(v) {
    let cur = /*HOW TO CALL GETTER HERE? */ + v
    UserDefaults.standard.set(cur, forKey: TICK)
}

if I use self
instead of /*HOW TO CALL GETTER HERE? */
, it doesn’t work.

You can simply call the value ( tick
) within its setter to get the old value:

public var tick: Int64 {
    get {
        return UserDefaults.standard.object(forKey: TICK) as? Int64 ?? 0
    }
    set(v) {
        let cur = tick + v
        UserDefaults.standard.set(cur, forKey: TICK)
    }
}

阅读原文...


Hello, buddy!

Border-radius and :hover state area issue

上一篇

小钢炮 Plus!Phanteks MG 210G 装机作业

下一篇

您也可能喜欢

评论已经被关闭。

插入图片
Getter inside setter in Swift 3

长按储存图像,分享给朋友