IOS: How does Objective-C code read static variables in Swift?

For example, let's say if I have a class with the static variable pi.

let pi: Double = 3.1415926 class MyClass { // ... } 

How can I enable Objective-C code to use the pi static variable? The projectName-Swift.h class will have automatically generated code like this (only a small example, not 100% accurate).

  SWIFT_CLASS("_MyClass") @interface MyClass : NSObject - (instancetype)init OBJC_DESIGNATED_INITIALIZER; @end 

So pi is not added to projectName-Swift.h. This is a small example of what is happening with my project, and maybe it should generate a static variable, and I'm missing something. Any advice or suggestions for correcting or completing this work will be appreciated.

+6
source share
1 answer

You cannot access Swift global variables in Objective C.

You have access to anything in the class or protocol that is marked with the @objc attribute if it is compatible with Objective-C. This excludes Swift features such as those listed here.

  • Generics
  • tuples
  • Enums defined in Swift
  • Structures defined in Swift
  • Top Level Functions Defined in Swift
  • Global variables defined in Swift
  • Types defined in Swift
  • Swift Style Options
  • Nested Types
  • Functions performed

Using Swift with Cocoa and Objective-C

Check out this post .

+18
source

Source: https://habr.com/ru/post/983668/


All Articles