Search code examples
iosswiftperformselector

How do you use perform(#selector(setter:)) to set the value of a property using swift 4?


I'm trying to run this code but it's yielding unexpected results.

class Test: NSObject {
    @objc var property: Int = 0
}

var t = Test()

t.perform(#selector(setter: Test.property), with: 100)
print(t.property)

The value being printed is some junk number -5764607523034233277. How can I set the value of a property using the perform method?


Solution

  • The performSelector:withObject: method requires an object argument, so Swift is converting the primitive 100 to an object reference.

    The setProperty: method (which is what #selector(setter: Test.property) evaluates to) takes a primitive NSInteger argument. So it is treating the object reference as an integer.

    Because of this mismatch, you cannot invoke setProperty: using performSelector:withObject: in a meaningful way.

    You can, however, use setValue:forKey: and a #keyPath instead, because Key-Value Coding knows how to convert objects to primitives:

    t.setValue(100, forKey: #keyPath(Test.property))