Swift globals and global functions in objective c

Here is the document about it

You’ll have access to anything within a class or protocol that’s
marked with the @objc attribute as long as it’s compatible with
Objective-C. This excludes Swift-only features such as those listed
here:

  • Generics
  • Tuples
  • Enumerations defined in Swift
  • 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

Global variables (including constants) are unaccessible from Objective-C.

Instead, you have to declare a class which has accessors for the global constants.

// Swift
public let CARDS = ["card1", "card2"]

@objc class AppConstant {
   private init() {}
   class func cards() -> [String] { return CARDS }
}

// Objective-C
NSArray *cards = [AppConstant cards];

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)