I'm having issues converting my NSData to NSString in swift. I'm using what I think is the correct command and format: NSString(data: <DATA>, encoding: <ENCODING>)
however whatever I do i keep ending up with a nil value. I am running the latest Xcode beta so I'm not sure if that is related but I'm hoping its a simple easy error that I've run into.
I've attached playground code as well as a screen capture.
Playground Code for Xcode 6.3 Beta 2 Build (6D532l)
import Foundation
//: # NSData to String Conversion Playground
//: ### Step 1
//: The first step is to take an array of bytes and conver them into a NSData object. The bytes are as follows:
var testBytes : [UInt8] = [0x14, 0x00, 0xAB, 0x45, 0x49, 0x1F, 0xEF, 0x15, 0xA8, 0x89, 0x78, 0x0F, 0x09, 0xA9, 0x07, 0xB0, 0x01, 0x20, 0x01, 0x4E, 0x38, 0x32, 0x35, 0x56, 0x20, 0x20, 0x20, 0x00]
//: ### Step 2
//: Convert the byte array into an **NSData** Object
var immutableData = NSData(bytes: testBytes, length: testBytes.count)
//: ### Step 3
//: Attempt to convert the **NSData** object into a string so it can be sent around as ascii. This for some reason seems to be failing, however.
var convertedString = NSString(data: immutableData, encoding: NSUTF8StringEncoding)
println("String = (convertedString)")
Results of Playgound
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…