2
 let tokenChars = UnsafePointer<CChar>(deviceToken.bytes)
    var tokenString = ""

    for i in 0..<deviceToken.length {
        tokenString += String(format: "%02.2hhx", arguments: [tokenChars[i]])
    }

    DDLogDebug("Device Token: \(tokenString)")

^ This no longer works. Mostly because there is no deviceToken.bytes anymore.

I tried to figure out how to use .withUnsafeBytes, but am failing.

Any help would be fantastic, I just need the string version of the device token, thanks!

JP.
  • 544
  • 5
  • 20
  • Swift 3 solutions here: http://stackoverflow.com/questions/7520615/how-to-convert-an-nsdata-into-an-nsstring-hex-string/38131414#38131414. – Martin R Sep 30 '16 at 19:46

1 Answers1

2

There is a one-line solution, deviceToken is Data

let tokenString = deviceToken.map{ String(format: "%02x", $0) }.joined()
vadian
  • 274,689
  • 30
  • 353
  • 361
  • You are a god among men @vadian :D When the time is up, I'll mark this as the best answer – JP. Sep 30 '16 at 19:47
  • 1
    There is nothing devine. I just read Swift Language Guide, Migration Guide and other documentation ;-) – vadian Sep 30 '16 at 19:51