swift的@objc总结
One can explicitly write @objc on any Swift declaration that can be expressed in Objective-C.
@objc相关的参量只能修饰类、类的成员、扩展以及只能被类实现的协议;
下面开列修饰的情况和说明
一、无修饰
NSObject-derived classes no longer infer @objc
A declaration within an NSObject-derived class will no longer infer @objc. For example:
class MyClass : NSObject {
func foo() { } // not exposed to Objective-C in Swift 4
}
Before Swift 4, the compiler made some Swift declarations automatically available to Objective-C. For example, if one subclassed from NSObject, the compiler created Objective-C entry points for all methods in such classes. The mechanism is called @objc inference.
In Swift 4, such automatic @objc inference is deprecated because it is costly to generate all those Objective-C entry points.
https://www.cnblogs.com/feng9exe/p/9675743.html
二、@objc修饰
1、修饰类:不再使用;
2、修饰扩展:扩展中的成员全部暴露给oc;
3、修饰协议:实现全部暴露;
4、修饰成员:暴露给oc;
To expose a group of members to Obj-C, you can use an @objc extension:
@objc extension ViewController{….}
说明:
1、NSObject-derived classes no longer infer @objc;
2、Other cases currently supported (e.g., a method declared in a subclass of NSObject) would no longer infer @objc, but one could continue to write it explicitly to produce Objective-C entry points.
3、扩展中的缺省实现无法继承,修饰为@objc后会被编译器进一步修饰为@objc and dynamic从而实现可继承性;
三、@objcMembers
If you have a class where you need all Obj-C compatible members to be exposed to Obj-C, you can mark the class as @objcMembers:
四、@nonobjc进行反向操作
五、原理与代价
The final observation is that there is a cost for each Objective-C entry point, because the Swift compiler must create a "thunk" method that maps from the Objective-C calling convention to the Swift calling convention and is recorded within Objective-C metadata. This increases the size of the binary (preliminary tests on some Cocoa[Touch] apps found that 6-8% of binary size was in these thunks alone, some of which are undoubtedly unused), and can have some impact on load time (the dynamic linker has to sort through the Objective-C metadata for these thunks).
六、使用代码示例
@objc protocol MyDelegate {
func bar()
}
class MyClass : MyDelegate {
/* inferred @objc */
func bar() { }
}
class SwiftClass { }
@objc extension SwiftClass {
func foo() { } // implicitly @objc
func bar() -> (Int, Int) // error: tuple type (Int, Int) not
// expressible in @objc. add @nonobjc or move this method to fix the issue
}
@objcMembers
class MyClass : NSObject {
func wibble() { } // implicitly @objc
}
@nonobjc extension MyClass {
func wobble() { } // not @objc, despite @objcMembers
}
参考资料:
https://www.cnblogs.com/feng9exe/p/9675743.html
https://github.com/apple/swift-evolution/blob/master/proposals/0160-objc-inference.md
swift的@objc总结的更多相关文章
- swift 与 @objc
Objective-C entry points https://github.com/apple/swift-evolution/blob/master/proposals/0160-objc-in ...
- Swift中 @objc 使用介绍
在swift 中 如果一个按钮添加点击方法 如果定义为Private 或者 定义为 FilePrivate 那么会在Addtaget方法中找不到私有方法 但是又不想把方法暴露出来,避免外界访问 ,那 ...
- swift和objc之對比
http://mobidev.biz/blog/swift_how_we_code_your_ios_apps_twice_faster
- iOS开发系列--Swift进阶
概述 上一篇文章<iOS开发系列--Swift语言>中对Swift的语法特点以及它和C.ObjC等其他语言的用法区别进行了介绍.当然,这只是Swift的入门基础,但是仅仅了解这些对于使用S ...
- 幼谈苹果新开发语言:Swift和苹果的用心
今天是个值得纪念的日子:因为苹果的WWDC大会.苹果的每次WWDC(全球开发者大会)举行都让我们像打了肾上腺素这么兴奋.幸福.惊叹.震撼.深思. 今年也不例外,最关键的是苹果带来了它的一门新开发语言: ...
- [ios][swift]swift混编
http://blog.csdn.net/iflychenyang/article/details/8876542(如何在Objective-C的头文件引用C++的头文件) 1.将.m文件扩展名改为. ...
- IOS开发之SWIFT进阶部分
概述 上一篇文章<iOS开发系列--Swift语言> 中对Swift的语法特点以及它和C.ObjC等其他语言的用法区别进行了介绍.当然,这只是Swift的入门基础,但是仅仅了解这些对于使用 ...
- ios Swift 国外资源
Swift国外资源汇总(No.1) 此类分享贴暂定每2天更新一次,主要目的是让大家能跟国外开发者们同步,共享知识和共同提高. 对于一些非常有价值的文章,大家有兴趣可以自行翻译(回贴跟我说一声,避免重复 ...
- Swift进阶
概述 访问控制 Swift命名空间 Swift和ObjC互相调用 Swift和ObjC映射关系 Swift调用ObjC ObjC调用Swift 扩展—Swift调用C 反射 扩展—KVO 内存管理 循 ...
随机推荐
- MySQL基准测试(二)--方法
MySQL基准测试(二)--方法 目的: 方法不是越高级越好.而应该善于做减法.至简是一种智慧,首先要做的是收集MySQL的各状态数据.收集到了,不管各个时间段出现的问题,至少你手上有第一时间的状态数 ...
- C++ 日期 & 时间
C++ 标准库没有提供所谓的日期类型.C++ 继承了 C 语言用于日期和时间操作的结构和函数. 为了使用日期和时间相关的函数和结构,需要在 C++ 程序中引用 头文件. 有四个与时间相关的类型:clo ...
- openWin和openFrame 设置透明背景
openWin简单点说就是:像是一个浏览器 openFrame就是对应openWin浏览器里面打开的每一个网页 有些操作只能在openWin里面执行,比如监听安卓返回事件,只能在openWin里面才有 ...
- java的四个基本特征
现实生活中的事物被抽象成对象,把具有相同属性和行为的对象被抽象成类,再从具有相同属性和行为的类中抽象出父类. 封装 隐藏对象的属性和实现细节,仅仅对外公开接口. 封装的有优点: 1.便于使用者正确.方 ...
- 本地计算机上的MySQL服务启动后停止。某些服务在未由其他服务或程序使用时将自动
重新安装MySQL数据库,由于安装的时候马虎,一路next(事实上,某些地方需要严格的配置,我忘记注意了),导致现在出了很多麻烦. 错误信息: 本地计算机上的MySQL服务启动后停止.某些服务在未由其 ...
- springMVC 拦截器源码解析
前言:这两天学习了代理模式,自然想到了 springmvc 的 aop 使用的就是动态代理,拦截器使用的就是 jdk 的动态代理.今天看了看源码,记录一下.转载请注明出处:https://www.cn ...
- API接口规范V1.0——制定好规范,才好合作开发
返回码规范: 统一六位 000000 表示成功! 参数相关返回码预留100000-199999:系统相关返回码预留200000-299999:数据中心310000-319999后续项目以此类推,后续根 ...
- Python 练习:三级菜单选择城市(二)
优化了上一个三级菜单选择城:http://www.cnblogs.com/klvchen/p/8646466.html info = { 'GuangDong':{ 'GuangZhou': ['Ti ...
- 浏览器兼容性问题——IE不支持却很实用的CSS属性Outline和Child
1. Outline(适用范围:鼠标悬浮hover加外边框) 我们在布局的时候,常常会因为添加边框border影响宽高的布局. 那么,outline是完美的替代品,因为它可以在不影响文档流的情况下呈现 ...
- python之约束, 异常处理, md5
1. 类的约束 1. 写一个父类. 父类中的某个方法要抛出一个异常 NotImplementedError (重点) 2. 抽象类和抽象方法 # 语法 # from abc import ABCMet ...