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

2Other 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总结的更多相关文章

  1. swift 与 @objc

    Objective-C entry points https://github.com/apple/swift-evolution/blob/master/proposals/0160-objc-in ...

  2. Swift中 @objc 使用介绍

    在swift 中 如果一个按钮添加点击方法 如果定义为Private  或者 定义为 FilePrivate 那么会在Addtaget方法中找不到私有方法 但是又不想把方法暴露出来,避免外界访问 ,那 ...

  3. swift和objc之對比

    http://mobidev.biz/blog/swift_how_we_code_your_ios_apps_twice_faster

  4. iOS开发系列--Swift进阶

    概述 上一篇文章<iOS开发系列--Swift语言>中对Swift的语法特点以及它和C.ObjC等其他语言的用法区别进行了介绍.当然,这只是Swift的入门基础,但是仅仅了解这些对于使用S ...

  5. 幼谈苹果新开发语言:Swift和苹果的用心

    今天是个值得纪念的日子:因为苹果的WWDC大会.苹果的每次WWDC(全球开发者大会)举行都让我们像打了肾上腺素这么兴奋.幸福.惊叹.震撼.深思. 今年也不例外,最关键的是苹果带来了它的一门新开发语言: ...

  6. [ios][swift]swift混编

    http://blog.csdn.net/iflychenyang/article/details/8876542(如何在Objective-C的头文件引用C++的头文件) 1.将.m文件扩展名改为. ...

  7. IOS开发之SWIFT进阶部分

    概述 上一篇文章<iOS开发系列--Swift语言> 中对Swift的语法特点以及它和C.ObjC等其他语言的用法区别进行了介绍.当然,这只是Swift的入门基础,但是仅仅了解这些对于使用 ...

  8. ios Swift 国外资源

    Swift国外资源汇总(No.1) 此类分享贴暂定每2天更新一次,主要目的是让大家能跟国外开发者们同步,共享知识和共同提高. 对于一些非常有价值的文章,大家有兴趣可以自行翻译(回贴跟我说一声,避免重复 ...

  9. Swift进阶

    概述 访问控制 Swift命名空间 Swift和ObjC互相调用 Swift和ObjC映射关系 Swift调用ObjC ObjC调用Swift 扩展—Swift调用C 反射 扩展—KVO 内存管理 循 ...

随机推荐

  1. 深度学习之PyTorch实战(1)——基础学习及搭建环境

    最近在学习PyTorch框架,买了一本<深度学习之PyTorch实战计算机视觉>,从学习开始,小编会整理学习笔记,并博客记录,希望自己好好学完这本书,最后能熟练应用此框架. PyTorch ...

  2. 【NET CORE微服务一条龙应用】第一章 网关使用与配置

    简介 微服务的系统应用中,网关系统使用的是ocelot,ocelot目前已经比较成熟了 ocelot就不做介绍了,等整体介绍完后再进行各类扩展介绍,ocelot源码地址:https://github. ...

  3. try、catch、finally详解,你不知道的异常处理

    介绍 不管是新手还是工作几年的老油条,对try{}catch{}来说是不陌生的.他可以来帮助我们获取异常信息,在try中的代码出现错误,火灾catch代码块中被捕获到.官方也给了详细的解释:. 抛出异 ...

  4. 性能监控(3)–linux下的iostat命令

    iostat可以显示cpu与磁盘信息,添加-d参数可以只显示磁盘信息

  5. PHP FastCGI进程管理器PHP-FPM的架构

    一个master进程,支持多个pool,每个pool由master进程监听不同的端口,pool中有多个worker进程.每个worker进程都内置PHP解释器,并且进程常驻后台,支持prefork动态 ...

  6. blfs(systemd版本)学习笔记-配置远程访问和管理lfs系统

    我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! 要实现远程管理和配置lfs系统需要配置以下软件包: 前几页章节脚本的配置:https://www.cnblogs.com/ren ...

  7. idea vue.js插件安装

    Vue.js for IntelliJ IDEA-based IDEs This plugin provides support for Vue.js in IntelliJ IDEA Ultimat ...

  8. Salesforce小知识:累计汇总字段类型

    累计汇总字段类型的定义 Salesforce中可以在两个对象之间建立关系.一个对象的某一条记录可以有若干条相关联的其他对象记录. 累计汇总字段可以将这些相关联的记录中的某些字段值汇总起来,显示给用户. ...

  9. Linux Linux内核参数调优

    Linux内核参数调优 by:授客 QQ:1033553122 关于调优的建议: 1.出错时,可以查看操作系统日志,可能会找到一些有用的信息 2.尽量不要“批量”修改内核参数,笔者就曾这么干过,结果“ ...

  10. Jni 线程JNIEnv,JavaVM,JNI_OnLoad(GetEnv返回NULL?FindClass返回NULL?)

    此文章是关于NDK线程的第二篇理论知识笔记.主要有两个点,如下: 1.pthread_create(Too many arguements, expected 1) ?2.线程中如何获取JNIEnv? ...