I have this code in my project (Xcode 9.4.1 (9F2000), Swift 3):
func application(_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let tokenParts = deviceToken.map { data -> String in
return String(format: "%02.2hhx", data)
}
let token = tokenParts.joined()
print("Device Token: \(token)\n")
}
func httpRequest() {
let postString = "token=\(token)"
}
This will print the device token for push notification.
But for let postString = "token=\(token)" I'm getting this:
Use of unresolved identifier 'token'
I guess I'm not able to access a variable from another function.
What can I do to access that variable in my function httpRequest() from the other function?
token. There is onetokenper invocation ofapplication(_:didRegisterForRemoteNotificationsWithDeviceToken:). Multiple threads could call that function at once, and each invocation would have its own set of local variables. Thus, you can't access variables within another function. You need to use parameters and return values to pass data around.NSURLQueryItemsgrokswift.com/building-urls