是否可以从objective-c文件中访问Swift全局变量? [重复]

时间:2022-09-07 19:25:01

This question already has an answer here:

这个问题在这里已有答案:

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代码扩展一个objective-c应用程序。所以我需要从.m文件中访问Swift代码。

I imported the generated Swift.h file and now I have access to the methods, but not to the global variables.

我导入了生成的Swift.h文件,现在我可以访问这些方法,但不能访问全局变量。

Is it possible to do that?

有可能吗?

Thanks!

谢谢!

1 个解决方案

#1


18  

According to Apple's docs on Swift and Objective-C type compatibility the answer is no:

根据Apple关于Swift和Objective-C类型兼容性的文档,答案是否定的:

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:

当您创建一个源自Objective-C类的Swift类时,可以从Objective-C自动获得与Objective-C兼容的类及其成员属性,方法,下标和初始化程序。这不包括仅限Swift的功能,例如此处列出的功能:

  • Generics
  • 泛型
  • Tuples
  • 元组
  • Enumerations defined in Swift without Int raw value type
  • 在没有Int原始值类型的Swift中定义的枚举
  • Structures defined in Swift
  • Swift中定义的结构
  • Top-level functions defined in Swift
  • Swift中定义的*函数
  • Global variables defined in Swift
  • Swift中定义的全局变量
  • Typealiases defined in Swift
  • 在Swift中定义的Typealiases
  • Swift-style variadics
  • Swift风格的可变参数
  • Nested types
  • 嵌套类型
  • Curried functions
  • 咖喱功能

#1


18  

According to Apple's docs on Swift and Objective-C type compatibility the answer is no:

根据Apple关于Swift和Objective-C类型兼容性的文档,答案是否定的:

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:

当您创建一个源自Objective-C类的Swift类时,可以从Objective-C自动获得与Objective-C兼容的类及其成员属性,方法,下标和初始化程序。这不包括仅限Swift的功能,例如此处列出的功能:

  • Generics
  • 泛型
  • Tuples
  • 元组
  • Enumerations defined in Swift without Int raw value type
  • 在没有Int原始值类型的Swift中定义的枚举
  • Structures defined in Swift
  • Swift中定义的结构
  • Top-level functions defined in Swift
  • Swift中定义的*函数
  • Global variables defined in Swift
  • Swift中定义的全局变量
  • Typealiases defined in Swift
  • 在Swift中定义的Typealiases
  • Swift-style variadics
  • Swift风格的可变参数
  • Nested types
  • 嵌套类型
  • Curried functions
  • 咖喱功能