[swift-users] How to write better Swift

Adrian Zubarev adrian.zubarev at devandartist.com
Mon Jul 10 10:40:01 CDT 2017


It should be willSet because in the example the job is executed before the new value is set.

class SomeClass {

    var status: Int {
        willSet {
            guard newValue != self.status { return }
            // do something here
        }
    }
}


-- 
Adrian Zubarev
Sent with Airmail

Am 10. Juli 2017 um 17:35:34, Geordie J via swift-users (swift-users at swift.org) schrieb:

Would "didSet" on a normal variable work for you?

var status: Int {
didSet { doSomething() }
}

Geordie


> Am 10.07.2017 um 17:34 schrieb Gerriet M. Denkmann via swift-users <swift-users at swift.org>:
>  
> This works (Xcode Version 8.3.2 (8E2002)):
>  
> class SomeClass
> {
> private var privateStatus: Int		
>  
> var status: Int
> {
> get{ return privateStatus }
> set(new)
> {
> if new == privateStatus {return}
>  
> … do something here …
>  
> privateStatus = new
> }
> }
> }
>  
> But is this “privateStatus” really necessary?
> If not, how can it be avoided?
>  
> Gerriet.
>  
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users at swift.org
https://lists.swift.org/mailman/listinfo/swift-users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170710/e2b026dc/attachment.html>


More information about the swift-users mailing list