是否可以从Objective-C文件访问Swift全局变量? [英] Is it possible to access Swift global variables from an objective-c file?
问题描述
我正在一个项目中,我必须使用一些Swift代码扩展一个object-c应用程序.因此,我需要从.m文件访问Swift代码.
I am working on a project where I must extend an objective-c application with some Swift code. So I need to access the Swift code from an .m file.
我导入了生成的Swift.h文件,现在我可以访问方法了,但是不能访问全局变量.
I imported the generated Swift.h file and now I have access to the methods, but not to the global variables.
有可能这样做吗?
谢谢!
推荐答案
According to Apple's docs on Swift and Objective-C type compatibility the answer is no:
创建从Objective-C类继承的Swift类时,可以从Objective-C自动获得与Objective-C兼容的类及其成员(属性,方法,下标和初始化程序).这不包括仅限Swift的功能,例如此处列出的功能:
When you create a Swift class that descends from an Objective-C class, the class and its members—properties, methods, subscripts, and initializers—that are compatible with Objective-C are automatically available from Objective-C. This excludes Swift-only features, such as those listed here:
- 泛型
- 故事
- 在Swift中定义的没有
Int
原始值类型的枚举 - 在Swift中定义的结构
- 在Swift中定义的顶级功能
- 在Swift中定义的全局变量
- 在Swift中定义的类型别名
- 快速样式的变数
- 嵌套类型
- 当前功能
- Generics
- Tuples
- Enumerations defined in Swift without
Int
raw value type - Structures defined in Swift
- Top-level functions defined in Swift
- Global variables defined in Swift
- Typealiases defined in Swift
- Swift-style variadics
- Nested types
- Curried functions
这篇关于是否可以从Objective-C文件访问Swift全局变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!