简体   繁体   中英

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM