AppsDev
AppsDev

Reputation: 12499

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?

Upvotes: 11

Views: 7453

Answers (2)

Bhoomi Jagani
Bhoomi Jagani

Reputation: 2423

Make one file like File -> ios -> Swift File. It will create Empty File.

Ex.

  import Foundation
  let USER:Int = 0
  let PROMOTER:Int = 1
  let NETWORKERROR_DESC = "Network not reachable.Please check  internet connection" as String
  let NETWORKERROR_Title = "Network Unavilable" as String

Now I can access USER from anywhere in application.

Upvotes: 3

Saad
Saad

Reputation: 8947

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

Upvotes: 8

Related Questions