【发布时间】:2014-12-30 04:58:23
【问题描述】:
我需要将一个变量从 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
}
这是我的全局类:
public class Globals {
var deviceToken = String()
init() {
//nothing
}
func setDeviceToken(s:String){
deviceToken = s
}
func getDeviceToken() -> String {
return deviceToken
}
}
如果我尝试从项目的其他文件中打印该值,我无法得到任何东西,只是一个空字符串。
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
【问题讨论】:
-
应用委托中应该保存多少数据?没有!曾经!不要这样做。这不是应用程序委托的用途。
-
没有必要生气,我只是一个初学者,我正在努力学习。 @雾迈斯特
标签: ios swift parameter-passing appdelegate