ios NSData 到 Swift 问题中的字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28866935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NSData to String in Swift Issues
提问by Jeef
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.
我在快速将 NSData 转换为 NSString 时遇到问题。我正在使用我认为正确的命令和格式:NSString(data: <DATA>, encoding: <ENCODING>)
但是无论我做什么,我都会以 nil 值结束。我正在运行最新的 Xcode 测试版,所以我不确定这是否相关,但我希望这是我遇到的一个简单的简单错误。
I've attached playground code as well as a screen capture.
我附上了操场代码以及屏幕截图。
Playground Code for Xcode 6.3 Beta 2 Build (6D532l)
Xcode 6.3 Beta 2 Build (6D532l) 的 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:
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
游乐场的结果
回答by Leo Dabus
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
Xcode 8.2?斯威夫特 3.0.2
func bytes2String(_ array: [UInt8]) -> String {
return String(data: Data(bytes: array, count: array.count), encoding: .utf8) ?? ""
}
Testing:
测试:
bytes2String(testBytes) // "Hello World"
回答by SwiftArchitect
Use valid UTF8 characters!
使用有效的 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)")
Your output will be:
"String = Optional(Hello World)"
您的输出将是:
"String = Optional(Hello World)"
回答by Water Not Words
If you just want hex values in string:
如果您只想要字符串中的十六进制值:
I just want a string of the hex values. I guess this is trying to decode as actual ascii! Duh!! – Jeef Mar 4 at 23:29
我只想要一串十六进制值。我想这是试图解码为实际的 ascii!呸!!– Jeef 3 月 4 日 23:29
The easiest way to do this is just use Swifts built in string interpolation.
最简单的方法是使用内置字符串插值的 Swifts。
let myHexString = "\(myNSDataObject)"
This will give you a string of hex with spaces between each two characters and surrounded by square brackets. Like this:
这将为您提供一个十六进制字符串,每两个字符之间有空格并用方括号括起来。像这样:
<a0 ff 21 4a>
You could format it with using the built in string methods:
您可以使用内置的字符串方法对其进行格式化:
myHexString.stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: "<>")).stringByReplacingOccurrencesOfString(" ", withString: "")
You will have then have a string containing: a0ff214a
然后您将有一个包含以下内容的字符串:a0ff214a
回答by Arben Pnishi
Using UTF8 in Swift 3.0
在 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)")