Objective-C代码可以在Class上调用swift扩展吗? [英] Can objective-c code call swift extension on Class?

查看:89
本文介绍了Objective-C代码可以在Class上调用swift扩展吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了一些帖子,我想我不能在swift下编写扩展名,而是从Objective-C代码中调用它,对吧?

@objc之类的属性仅支持方法,类,协议?

解决方案

您可以编写一个Swift扩展并将其用于Objective-C代码.通过XCode 6.1.1进行了测试.

您需要做的只是:

  • 在Swift中创建扩展(无@objc注释)

  • (在Objective-C类中,
  • #import "ProjectTarget-Swift.h",其中"ProjectTarget"代表Swift扩展所关联的XCode目标)

  • 从Swift扩展中调用方法

更新: 就像@Rizwan Ahmed提到的那样,您需要添加@objc批注:

从Swift 4.0.3开始,如果要使用@objc批注 目标C类文件中的扩展名.

I searched some posts, I think I cannot write an extension under swift, and call it from Objective-C code, right ?

@objc like attributes only support methods, class, protocols ?

解决方案

You can write a Swift extension and use it in Objective-C code. Tested with XCode 6.1.1.

All you need to do is:

  • create your extension in Swift (no @objc annotation)

  • #import "ProjectTarget-Swift.h" in your Objective-C class (where "ProjectTarget" represents the XCode target the Swift extension is associated with)

  • call the methods from the Swift extension

Update: As of @Rizwan Ahmed mentioned, you need to add @objc annotation:

As of Swift 4.0.3, the @objc annotation is needed if you want to use the extension in Objective C class files.

这篇关于Objective-C代码可以在Class上调用swift扩展吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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