Group Group Group Group Group Group Group Group Group

Enum with computed property

how do I not use a static var and return true or false here as it changes UI for every cell rather than for selected cell - obviously :confused:

could use UserDefaults but using that for selection/deselction of cell sounds weird . . .? idk -> using chained delegation to send this cell from the view model to the controller, hence the issue

enum CellType: CustomStringConvertible {
  static var returnValue: Bool = false

  var selected: Bool {
    get {
      return returnValue
    }

    nonmutating set {
      returnValue = newValue
    }
  }
}

Should have posted this chunk of code

enum CellType {
  static var returnValue: Bool = false

  var selected: Bool {
    get {
      return CellType.returnValue
    }

    nonmutating set {
      CellType.returnValue = newValue
    }
  }
}
var selected: Bool {
  get { Self.returnValue }
  nonmutating set { Self.returnValue = newValue }
}

Also, it’s fine to give them the same name.

  static var selected = false

  var selected: Bool {
    get { Self.selected }
    nonmutating set { Self.selected = newValue }
  }
1 Like

doesn’t quite solve my problem as it is not even associated with this at this point, haha - but using Self looks better (any other plusses?) than me using CellType. [not in the piece of code I posted]

Thanks @jessycatterwaul

Hi @lganti,
When you create a class or a struct, all properties are part of the instance. Instance is when you instantiate a class. For example,

struct One {
   var name: String
}

let one = One(name: "Uno")
print(one.name) // Uno

However when you use static variables, they are unavailable for the structure or its functions/methods.

static properties or functions are similar to class functions, i.e. they are available to the structure or class not the instance.

struct Two {
   static var language = "Italian"
   var name: String
}

let two = Two(name: "Dos")
print(two.name) // Dos
print(two.langauge) // ERROR - static properties are not available in the instance
print(Two.language) // Italian - Available on the structure/class

lastly, the Self is not the same as self as lowercased self refers to the instance and the uppercased Self refers to the structure and provides the static properties and functions.

cheers,

1 Like

yes, appreciate the response : )

on another note - looks like I did not frame my question properly and I posted incorrect chunk of code

Hi @lewis-h,
There seems to be a disconnect between your response and the original question, do you want to fill in the gaps to what is it that you are trying to resolve.

cheers,

@lganti Do you still have issues with this?

jessy and jayant helped out, resolved