Swift 中的 Objective-C id 相当于什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24005678/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:15:44  来源:igfitidea点击:

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

objective-cswift

提问by Doug Richardson

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?

我正在尝试使用 @IBAction 将按钮单击事件绑定到 Swift 方法。在 Objective-C 中,IBAction 的参数类型是 id。Swift 中 id 的等价物是什么?

回答by Doug Richardson

Swift 3

斯威夫特 3

Any, if you know the sender is never nil.

Any,如果你知道发件人是永远nil

@IBAction func buttonClicked(sender : Any) {
    println("Button was clicked", sender)
}

Any?, if the sender could be nil.

Any?,如果发件人可以nil

@IBAction func buttonClicked(sender : Any?) {
    println("Button was clicked", sender)
}

Swift 2

斯威夫特 2

AnyObject, if you know the sender is never nil.

AnyObject,如果你知道发件人是永远nil

@IBAction func buttonClicked(sender : AnyObject) {
    println("Button was clicked", sender)
}

AnyObject?, if the sender could be nil.

AnyObject?,如果发件人可以nil

@IBAction func buttonClicked(sender : AnyObject?) {
    println("Button was clicked", sender)
}

回答by Nilesh Patel

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

将某些 Objective-C 核心类型重新映射到它们在 Swift 中的替代项,例如 NSString 到 String

将某些 Objective-C 概念重新映射到 Swift 中的匹配概念,例如指向可选项的指针