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

Objective CSwift

Objective C Problem Overview


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?

Objective C Solutions


Solution 1 - Objective C

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

Solution 2 - Objective C

AnyObject

Other mapping type,

> Remap certain Objective-C core types to their alternatives in Swift, > like NSString to String > > Remap certain Objective-C concepts to matching concepts in Swift, like > pointers to optionals

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDoug RichardsonView Question on Stackoverflow
Solution 1 - Objective CDoug RichardsonView Answer on Stackoverflow
Solution 2 - Objective CNilesh PatelView Answer on Stackoverflow