何时在swift代码中使用@objc? [英] when to use @objc in swift code?

查看:158
本文介绍了何时在swift代码中使用@objc?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是swift的新手,我看到一些方法,如:

I am new to swift, and I see some methods like:

@objc private func doubleTapGestureRecognized(recognizer: UITapGestureRecognizer)

我想知道何时使用@objc?我读了一些文档,但他们说当你希望它在Objective-C中可以调用时,你应该添加@objc标志

I was wondering, when to use @objc? I read some documents, but they are saying when you want it to be callable in Objective-C, you should add @objc flag

但是,这是一个私有函数swift,@ obj做了什么?

However, this is a private function in swift, what does the @obj do?

推荐答案

私人意味着它只在Swift中可见。
所以使用@objc在Objective-C中可见。
如果你有一个func来选择一个私有函数在swift中,它是必需的。

private mean it visible only in Swift. so use @objc to visible in Objective-C. If you have a func to selector a private func in swift, it is required.


@objc属性使你的Swift Objective-C和Objective-C运行时可用的API。

The @objc attribute makes your Swift API available in Objective-C and the Objective-C runtime.

参见:
https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html

https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithObjective-CAPIs.html

这篇关于何时在swift代码中使用@objc?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆