Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the equivalent of an Objective-C id in Swift?

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?

like image 339
Doug Richardson Avatar asked Jun 03 '14 01:06

Doug Richardson


People also ask

Can I use Objective-C 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.

What is Objective-C ID?

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.

Is Swift a superset of Objective-C?

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.


1 Answers

Swift 3

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)
}

Swift 2

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)
}
like image 59
Doug Richardson Avatar answered Oct 09 '22 21:10

Doug Richardson