我在AppDelegate中创建此变量以存储deviceToken,然后将其传递给另一个类
var deviceTokenToPass:NSData?
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
deviceTokenToPass = deviceToken
}
在另一个类中,我试图将deviceToken传递给Parse.com,但在deviceToken传递值的行中出现异常
let delegate = UIApplication.sharedApplication().delegate as! AppDelegate
let deviceToken = delegate.deviceTokenToPass
var currentInstallation = PFInstallation.currentInstallation()
currentInstallation.setDeviceTokenFromData(deviceToken) // Exception in this line
currentInstallation.channels = ["global"]
currentInstallation.saveInBackground()
当我将其放入AppDelegate中但我想将此数据存储在另一个类中时,这行同样适用
最佳答案
你需要拆开它
if let deviceToken = delegate.deviceTokenToPass
{
var currentInstallation = PFInstallation.currentInstallation()
currentInstallation.setDeviceTokenFromData(deviceToken)
currentInstallation.channels = ["global"]
currentInstallation.saveInBackground()
}
关于ios - 将AppToken中的deviceToken swift 传递给另一个类时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37881997/