Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to migrate Objective-C extern constants to Swift

I have an Objective-C class in a .h file similar to this:

@interface GlobalTags : NSObject
   extern const BOOL showMessages;
@end

and a .m file similar to this:

@implementation GlobalTags
   const BOOL showMessages = YES;
@end

In summary, what I want is to have several global variables with no need to instance a class, just to access them by importing their file. What is the best way to do this in Swift?

Thanks

EDIT: regarding getting Swift global variables from Objective-C code, it is not possible (see this post).

To make such global variables visible in Objective-C code, I'm doing this in the Swift file:

class GlobalTags: NSObject {
   static let showMessages: Bool = true;
}

Would this be the correct/best way to do?

like image 967
AppsDev Avatar asked Apr 16 '15 10:04

AppsDev


1 Answers

it's simple. In your Constants.swift file:

let kStringConstKey : String = "YourKey"
let kBoolConstKey : Bool = true
let kIntConstKey : Int = 34
let kDoubleConstKey : Double = 25.0
like image 160
Saad Avatar answered Sep 24 '22 14:09

Saad