How to write better Swift


(Gerriet M. Denkmann) #1

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.


(Geordie J) #2

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@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Adrian Zubarev) #3

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@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@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Gerriet M. Denkmann) #4

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

willSet/didSet usually is a good solution (which I had forgotten about).

But in my case it seems not to help - see the corrected code below

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

Geordie

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}

        oldDerived = derivedStatus // depends on status
        privateStatus = new
        newDerived = derivedStatus

        if newDerived != oldDerived … do something …

        … do something more here …

    }
  }
}

But is this “privateStatus” really necessary?
If not, how can it be avoided?

Quincey had an excellent idea: one should be able to write:

var status: Int
{
   var privateStatus: Int // makes it much clearer that privateStatus really only belongs to status

  get{ return privateStatus }
  set(new)
  {
    …
    privateStatus = new
  }
}

Gerriet.

···

On 10 Jul 2017, at 22:35, Geordie J via swift-users <swift-users@swift.org> wrote:

Am 10.07.2017 um 17:34 schrieb Gerriet M. Denkmann via swift-users <swift-users@swift.org>: