Is swift automatically implements(creates) the setter (set<Key>()) method for setValue(_:forKey:) method?

HI everyone! I am really confused about this question , now i am parsing JSON file, by means of setValuesForKeys(:) function and everythink works fine, and i know setValuesForKeys(:) uses setValue(:forKey:) method for every property of my class. So when setValue(:forKey:) is called by setValuesForKeys(:), setValue(:forKey:) is searching for set() method to carrying out its task. And due to debug information i know that setValue(_:forKey:) is really founds this (set()) method! but i did not create it. So my question is , does swift (or objc) creates it is automatically or automatically during run time or anything else?



class Video: NSObject {

var thumbnail_image_name: String?

var title: String? var number_of_views: NSNumber?

var uploadDate: NSDate? var duration: NSNumber?

var channel: Channel?


override func setValue(_ value: Any?, forKey key: String) {

if key == "channel" { let channelDictionary = dictionary["channel"] as! [String: Any]

let channel = Channel()

channel.setValuesForKeys(channelDictionary) video.channel = channel }

else

{ super.setValue(value, forKey: key) }

}

}

class Channel: NSObject {

var name: String?

var profile_image_name: String?

}

https://stackoverflow.com/questions/46757955/is-swift-automatically-implementscreates-the-setter-setkey-method-for-se 

here my question more concrete. Thanks for everyone I really confused((((  

Replies

— "setValue(_:forKey:)" will look for an Obj-C property whose name matches the key.


— An Obj-C always has a getter method (and a setter method if it's mutable).


— A Swift "@objc" property is really an Obj-C property underneath.


Putting all that together, a Swift @objc property must have a getter (and setter, if necessary) synthesized by the compiler. That's basically why your code worked.


Note that it must be an @objc property. If you're using Swift 3, properties of @objc classes (like subclasses of NSObject) are @objc by default. In Swift 4, they are not, and you should mark them "@objc" explicitly — and mark them "dynamic" too, in most cases.