NSData在Swift问题中的string
我有问题转换我的NSData到NSString在迅速。 我正在使用我认为是正确的命令和格式: NSString(data: <DATA>, encoding: <ENCODING>)
但是无论我做什么,我总是以零值结束。 我正在运行最新的Xcodetesting版,所以我不确定这是否是相关的,但我希望它是一个简单的错误,我已经遇到。
我附上了操场代码以及屏幕截图。
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)")
Playgound的结果
let testBytes : [UInt8] = [0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64] func bytes2String(array:[UInt8]) -> String { return String(data: NSData(bytes: array, length: array.count), encoding: NSUTF8StringEncoding) ?? "" }
Xcode 8.2•Swift 3.0.2
func bytes2String(_ array: [UInt8]) -> String { return String(data: Data(bytes: array, count: array.count), encoding: .utf8) ?? "" }
testing:
bytes2String(testBytes) // "Hello World"
使用有效的UTF8字符!
// Playground - noun: a place where people can play import UIKit var str = "Hello, playground" 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: // Hello World var testBytes : [UInt8] = [0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64] //: ### 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)")
您的输出将是: "String = Optional(Hello World)"
如果你只想在string中使用hex值:
我只想要一个hex值的string。 我想这是试图解码为真正的ASCII! 咄! – Jeef 3月4日23:29
最简单的方法就是使用string插值中的Swifts。
let myHexString = "\(myNSDataObject)"
这将给你一个hex的string,每两个字符之间有空格,并用方括号括起来。 喜欢这个:
<a0 ff 21 4a>
你可以使用内置的string方法来格式化它:
myHexString.stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: "<>")).stringByReplacingOccurrencesOfString(" ", withString: "")
您将有一个string包含:a0ff214a
在Swift 3.0中使用UTF8
let 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] let immutableData = NSData(bytes: testBytes, length: testBytes.count) let convertedString = NSString(data: (immutableData as NSData) as Data, encoding:String.Encoding.utf8.rawValue) print("String = \(convertedString)")
var tokenString:String = deviceToken.description.stringByTrimmingCharactersInSet(NSCharacterSet.init(charactersInString: "<>")) tokenString = tokenString.stringByReplacingOccurrencesOfString(" ", withString: "")