15

I need to pass a variable from the AppDelegate to another class that I have created to hold global variables of the project and I'm not able to find a way to make it work.

This is the code in the AppDelegate:

func application(application: UIApplication!, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData!) {
    println("Device's token is: \(deviceToken)")

    //Global Variables Class Instance
    let globals:Globals = Globals()

    globals.setDeviceToken("test1") //method1 not working
    globals.deviceToken = "test2"   //method2 not working
}

This is my Globals Class:

public class Globals {
    var deviceToken = String()

    init() {
        //nothing
    }

    func setDeviceToken(s:String){
        deviceToken = s
    }

    func getDeviceToken() -> String {
        return deviceToken
    }
}

If i try to print the value, from other files of the project, I'm not able to get anything, just an empty string.

class ViewController: UIViewController {

    //Global Variables Class Instance
    let globals:Globals = Globals()

    override func viewDidLoad() {
        println(globals.getDeviceToken())  //return empty string
        println(globals.deviceToken)       //return empty string
Matei Suica
  • 859
  • 1
  • 7
  • 17
MeV
  • 3,761
  • 11
  • 45
  • 78
  • How much data should be held in the app delegate? NONE! EVER! Don't do it. It isn't what the app delegate is for. – Fogmeister Nov 03 '14 at 14:38
  • 10
    There is no need to get upset, I'm just a beginner and I'm trying to learn. @Fogmeister – MeV Nov 06 '14 at 11:05

2 Answers2

15

There are several patterns you can use to achieve what you want

  1. You could access the AppDelegate through the UIApplication:

    let delegate = UIApplication.sharedApplication().delegate as AppDelegate
    let deviceToken = delegate.deviceToken
    
  2. Look into singletons. A quick google search for 'Swift singleton' will get you a long way. The first result:

    class SingletonB {
    
            class var sharedInstance : SingletonB {
            struct Static {
                static let instance : SingletonB = SingletonB()
            }
            return Static.instance
        }
    }
    

Then use sharedInstance to instantiate the singleton anywhere and access the same variables.

The first one is quick and dirty, so for more serious projects I would recommend the singleton pattern.

There are probably a million ways to do this, but this should get you started

(More at this link, which explores a few ways to implement singletons: https://github.com/hpique/SwiftSingleton )

Bob Vork
  • 2,927
  • 28
  • 32
  • Thanks for your reply. I never used singleton and need to read some documentation about it. But the code looks very similar to the one I was using for Globals. @Bob Vork – MeV Nov 03 '14 at 14:53
  • As documented in the linked-to article, the above rather convoluted way of creating a singleton is not needed if you're using Swift 1.2 or later. – RenniePet Mar 03 '17 at 06:08
5

I simply solved my problem using NSUserDefaults

in the AppDelegate:

NSUserDefaults.standardUserDefaults().setObject(deviceToken, forKey: "deviceToken")
NSUserDefaults.standardUserDefaults().synchronize()

From other classes:

NSUserDefaults.standardUserDefaults().objectForKey("deviceToken")

Honestly I don't know if this is a good way to do it but it's working

Matei Suica
  • 859
  • 1
  • 7
  • 17
MeV
  • 3,761
  • 11
  • 45
  • 78