Search code examples
swiftdictionaryobjectvarcombine

combine imageview and background image into one string


I am looking for a way to write swift code faster. SO I am wondering if there is i can combine

    grassColor.backgroundColor

Into into something like

   let combine = grassColor.backgroundColor

So I could code this instead of have to type grassColor.backgroundColor = .red every time.

@objc func fanColorRed(){
    combine = .red

}

@objc func fanColorBlue(){
    combine = .blue
 }
@objc func fanColorYellow(){
    combine = .yellow

 }
@objc func fanColorOrange(){
    combine = .orange

 }

Solution

  • You might want to take a look at Swift's computed properties.

    var combine: NSColor {
      get {
        return grassColor.backgroundColor
      }
      set {
        grassColor.backgroundColor = newValue
      }
    }