Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set Swift values by property name?

Objective-C has a setValue method which allows the developer to set a specific value to a field by it's name.

How can I do that in Swift without inheriting from NSObject and actually use the setValue method?

like image 409
YogevSitton Avatar asked Sep 21 '15 12:09

YogevSitton


1 Answers

You can use KVC to do so, afaik. Have a look at this cool example: https://www.raywenderlich.com/163857/whats-new-swift-4

struct Lightsaber {
  enum Color {
    case blue, green, red
  }
  let color: Color
}

class ForceUser {
  var name: String
  var lightsaber: Lightsaber
  var master: ForceUser?

  init(name: String, lightsaber: Lightsaber, master: ForceUser? = nil) {
    self.name = name
    self.lightsaber = lightsaber
    self.master = master
  }
}

let sidious = ForceUser(name: "Darth Sidious", lightsaber: Lightsaber(color: .red))
let obiwan = ForceUser(name: "Obi-Wan Kenobi", lightsaber: Lightsaber(color: .blue))
let anakin = ForceUser(name: "Anakin Skywalker", lightsaber: Lightsaber(color: .blue), master: obiwan)

// Use keypath directly inline and to drill down to sub objects
let anakinSaberColor = anakin[keyPath: \ForceUser.lightsaber.color]  // blue

// Access a property on the object returned by key path
let masterKeyPath = \ForceUser.master
let anakinMasterName = anakin[keyPath: masterKeyPath]?.name  // "Obi-Wan Kenobi"

// AND HERE's YOUR ANSWER
// Change Anakin to the dark side using key path as a setter
anakin[keyPath: masterKeyPath] = sidious
anakin.master?.name // Darth Sidious
like image 67
inigo333 Avatar answered Sep 18 '22 02:09

inigo333