How to initialise a string from NSData in Swift
This is the implemented code needed:
in Swift 3.0:
var dataString = String(data: fooData, encoding: String.Encoding.utf8)
or just
var dataString = String(data: fooData, encoding: .utf8)
Older swift version:
in Swift 2.0:
import Foundationvar dataString = String(data: fooData, encoding: NSUTF8StringEncoding)
in Swift 1.0:
var dataString = NSString(data: fooData, encoding:NSUTF8StringEncoding)
Swift 2.0
It seems that Swift 2.0 has actually introduced the String(data:encoding:)
as an String extension when you import Foundation
. I haven't found any place where this is documented, weirdly enough.
(pre Swift 2.0) Lightweight extension
Here's a copy-pasteable little extension without using NSString
, let's cut the middle-man.
import Foundationextension NSData{ var byteBuffer : UnsafeBufferPointer<UInt8> { get { return UnsafeBufferPointer<UInt8>(start: UnsafeMutablePointer<UInt8>(self.bytes), count: self.length) }}}extension String{ init?(data : NSData, encoding : NSStringEncoding) { self.init(bytes: data.byteBuffer, encoding: encoding) }}// Playground testlet original = "Nymphs blitz quick vex dwarf jog"let encoding = NSASCIIStringEncodingif let data = original.dataUsingEncoding(encoding){ String(data: data, encoding: encoding)}
This also give you access to data.byteBuffer
which is a sequence type, so all those cool operations you can do with sequences also work, like doing a reduce { $0 &+ $1 }
for a checksum.
Notes
In my previous edit, I used this method:
var buffer = Array<UInt8>(count: data.length, repeatedValue: 0x00)data.getBytes(&buffer, length: data.length)self.init(bytes: buffer, encoding: encoding)
The problem with this approach, is that I'm creating a copy of the information into a new array, thus, I'm duplicating the amount of bytes (specifically: encoding size * data.length
)