In ObjC you could simply invoke a class method using the class method from NSObject
.
[Machine performSelector:@selector(calculate:) withObject:num];
But how do you do this in Swift (2.2)?
@objc(Machine)
class Machine: NSObject {
static func calculate(param: NSNumber) -> String {
if param.integerValue > 5 {
return "42"
}
return "42" // there is only 1 answer to all the questions :D
}
}
if let aClass = NSClassFromString("Machine") {
let sel = #selector(Machine.calculate(_:))
let num = NSNumber(integer: 1337)
let answer = aClass.performSelector(sel, withObject: num) // compiler error
// let answer = aClass.calculate(num) // <-- this works
print(answer)
}
With above code I'm getting the following compiler error:
error: cannot invoke 'performSelector' with an argument list of type '(Selector, withObject: NSNumber)'
What am I missing here?
Aucun commentaire:
Enregistrer un commentaire