How do you output the value of NSData
in Swift? Is it NSLog
, print
or println
?
It seems I can just print the address of the object, and not the actual contents of it.
Thank-you
In Swift, you can print a variable or a constant to the screen using the print() function.
A static byte buffer that bridges to Data ; use NSData when you need reference semantics or other Foundation-specific behavior. iOS 2.0+ iPadOS 2.0+ macOS 10.0+ Mac Catalyst 13.0+ tvOS 9.0+ watchOS 2.0+
Data in Swift 3 is a struct that conforms to collection protocol. It is a collection of bytes ( [UInt8] array of unsigned integer 8 bits 0-255). – Leo Dabus.
So after trying a number of recommendations around the site, this is what actually ended up working for me.
Swift 3
let string1 = String(data: jsonData, encoding: String.Encoding.utf8) ?? "Data could not be printed" print(string1)
Swift 1
let string1 = NSString(data: jsonData, encoding: NSUTF8StringEncoding) println(string1)
Try NSString
let theString:NSString = NSString(data: someNSData, encoding: NSASCIIStringEncoding) println(theString)
Reference
convenience init(data: NSData, encoding: UInt)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With