Here is the old Objective C code (token is NSData):
const unsigned *tokenBytes = [credentials.token bytes];
NSString *hexToken = [NSString stringWithFormat:@"%08x%08x%08x%08x%08x%08x%08x%08x",
ntohl(tokenBytes[0]), ntohl(tokenBytes[1]), ntohl(tokenBytes[2]),
ntohl(tokenBytes[3]), ntohl(tokenBytes[4]), ntohl(tokenBytes[5]),
ntohl(tokenBytes[6]), ntohl(tokenBytes[7])];
Here is how I converted it to Swift:
let tokenBytes = credentials.token.withUnsafeBytes { (bytes: UnsafePointer<[UInt]>) -> [UInt] in
return bytes[0] // Crash here
}
let hexToken = String(format: "%08x%08x%08x%08x%08x%08x%08x%08x",
UInt(bigEndian: tokenBytes[0]), UInt(bigEndian: tokenBytes[1]),
UInt(bigEndian: tokenBytes[2]), UInt(bigEndian: tokenBytes[3]),
UInt(bigEndian: tokenBytes[4]), UInt(bigEndian: tokenBytes[5]),
UInt(bigEndian: tokenBytes[6]), UInt(bigEndian: tokenBytes[7])
)
Does anyone know what I'm doing wrong? As far as I know I converted from bytes
to withUnsafeBytes
correctly, but it seems like I'm mistaken.