将数据从appDelegate传递到另一个类

时间:2022-05-25 10:20:21

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.

我需要将一个变量从AppDelegate传递给另一个我创建来保存项目全局变量的类,我无法找到使它工作的方法。

This is the code in the AppDelegate:

这是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

***UPDATE: To those who voted down my question: Thank you very much. Sorry if I'm not an expert of Swift (yet) and sorry if I'm using * to ask something I don't know!

***更新:对于那些投票否决我的问题的人:非常感谢。对不起,如果我还不是Swift的专家,如果我用*来询问我不知道的事情,我很抱歉!

2 个解决方案

#1


14  

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

有几种模式可以用来实现您想要的

  1. You could access the AppDelegate through the UIApplication:

    你可以通过UIApplication访问AppDelegate:

    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.

然后使用sharedInstance实例化singleton,并访问相同的变量。

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 )

(更多信息请参见这个链接,它探讨了实现单例的几种方法:https://github.com/hpique/SwiftSingleton)

#2


5  

I simply solved my problem using NSUserDefaults

我只是用NSUserDefaults来解决我的问题

in the AppDelegate:

在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

老实说,我不知道这是不是一个好方法,但它确实有效

***UPDATE: To those who voted down my question: Thank you very much. Sorry if I'm not an expert of Swift (yet) and sorry if I'm using * to ask something I don't know!

***更新:对于那些投票否决我的问题的人:非常感谢。对不起,如果我还不是Swift的专家,如果我用*来询问我不知道的事情,我很抱歉!

#1


14  

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

有几种模式可以用来实现您想要的

  1. You could access the AppDelegate through the UIApplication:

    你可以通过UIApplication访问AppDelegate:

    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.

然后使用sharedInstance实例化singleton,并访问相同的变量。

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 )

(更多信息请参见这个链接,它探讨了实现单例的几种方法:https://github.com/hpique/SwiftSingleton)

#2


5  

I simply solved my problem using NSUserDefaults

我只是用NSUserDefaults来解决我的问题

in the AppDelegate:

在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

老实说,我不知道这是不是一个好方法,但它确实有效

***UPDATE: To those who voted down my question: Thank you very much. Sorry if I'm not an expert of Swift (yet) and sorry if I'm using * to ask something I don't know!

***更新:对于那些投票否决我的问题的人:非常感谢。对不起,如果我还不是Swift的专家,如果我用*来询问我不知道的事情,我很抱歉!