Swift与Objective-C API交互(三)
Swift类型兼容性 定义一个继承自 @objc还可以用来指定Swift中的属性或方法在Objective-C中的名字,比如Swift支持Unicode名字,包括使用中文等Objective-C不兼容的字符。还有给Swift中定义的函数指定一个Selectorde名字。 @objc(Squirrel)
class 长沙戴维营教育 {
@objc(hideNuts:inTree:)
func 欢迎光临(Int,姓名: String) {
/* ... */
}
}
当 Objective-C选择器(Selector) Objective-C的选择器是方法的一个引用。在Swift中对应的是 <code class="ocaml hljs" data-origin="" <pre><code="" uikit"="" style="margin: 0px; padding: 0px; border: 0px; font-size: inherit; font-variant: inherit; font-weight: bold; line-height: inherit; vertical-align: baseline; color: rgb(110,107,94); font-family: Consolas,monospace !important;">import UIKit
class MyViewController: UIViewController {
let myButton = UIButton(frame: CGRect(x: 0,y: 100,height: 50))
init(nibName nibNameOrNil: String!,bundle nibBundleOrNil: NSBundle!)
{
super.init(nibName: nibName,bundle: nibBundle)
myButton.targetForAction("tappedButton:",withSender: self)
}
func tappedButton(sender: UIButton!) {
println("tapped button")
}
}
如果Swift类继承自Objective-C的类,则它里面的方法和属性都能够作为Objective-C的选择器使用。而如果不是Objective-C的子类,需要使用 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |