Objective-C 代码可以调用 Swift 类扩展吗? [英] Can Objective-C code call Swift class extensions?

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

问题描述

我搜了一些帖子,我觉得我不能在Swift下写一个扩展,从Objective-C代码中调用它,对吗?

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

@objc 之类的属性只支持方法、类、协议?

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

推荐答案

您可以编写 Swift 扩展并在 Objective-C 代码中使用它.使用 XCode 6.1.1 测试.

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

您需要做的就是:

  • 在 Swift 中创建您的扩展(@objc 自 Swift 4.0.3 起需要注释)

  • create your extension in Swift (@objc annotation needed since Swift 4.0.3)

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

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

从 Swift 扩展中调用方法

call the methods from the Swift extension

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

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