Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing NSData in Swift?

Tags:

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

like image 667
User Avatar asked Sep 19 '14 21:09

User


People also ask

How do you print a variable in Swift?

In Swift, you can print a variable or a constant to the screen using the print() function.

What is NSData in iOS?

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+

What is data in Swift?

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.


2 Answers

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) 
like image 109
User Avatar answered Oct 04 '22 21:10

User


Try NSString

let theString:NSString = NSString(data: someNSData, encoding: NSASCIIStringEncoding) println(theString) 

Reference

convenience init(data: NSData, encoding: UInt) 
like image 43
Maxim Shoustin Avatar answered Oct 04 '22 22:10

Maxim Shoustin