I'm trying to use an @IBAction to tie up a button click event to a Swift method. In Objective-C the parameter type of the IBAction is id. What is the equivalent of id in Swift?
You can use Objective-C and Swift files together in a single project, no matter which language the project used originally. This makes creating mixed-language app and framework targets as straightforward as creating an app or framework target written in a single language.
id is the generic object pointer, an Objective-C type representing "any object". An instance of any Objective-C class can be stored in an id variable.
Unlike Objective-C, which is a proper superset of C, Swift has been built as an entirely new language. Swift cannot compile C code because the syntax is not compatible.
Any
, if you know the sender is never nil
.
@IBAction func buttonClicked(sender : Any) {
println("Button was clicked", sender)
}
Any?
, if the sender could be nil
.
@IBAction func buttonClicked(sender : Any?) {
println("Button was clicked", sender)
}
AnyObject
, if you know the sender is never nil
.
@IBAction func buttonClicked(sender : AnyObject) {
println("Button was clicked", sender)
}
AnyObject?
, if the sender could be nil
.
@IBAction func buttonClicked(sender : AnyObject?) {
println("Button was clicked", sender)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With