![](/img/trans.png)
[英]convert JSONresponse to NSDATA or NSDICTIONARY in swift4
[英]NSDictionary to NSData and NSData to NSDictionary in Swift
我不確定我是否錯誤地使用了字典或數據對象或兩者。 我正在嘗試習慣切換到 swift,但我遇到了一些麻煩。
var dictionaryExample : [String:AnyObject] =
["user":"UserName",
"pass":"password",
"token":"0123456789",
"image":0] // image should be either NSData or empty
let dataExample : NSData = dictionaryExample as NSData
我需要NSDictionary
編碼為NSData
對象以及獲取該NSData
對象並將其解碼為NSDictionary
。
非常感謝任何幫助,謝謝。
您可以使用NSKeyedArchiver
和NSKeyedUnarchiver
swift 2.0+ 的示例
var dictionaryExample : [String:AnyObject] = ["user":"UserName", "pass":"password", "token":"0123456789", "image":0]
let dataExample : NSData = NSKeyedArchiver.archivedDataWithRootObject(dictionaryExample)
let dictionary:NSDictionary? = NSKeyedUnarchiver.unarchiveObjectWithData(dataExample)! as? NSDictionary
斯威夫特3.0
let dataExample: Data = NSKeyedArchiver.archivedData(withRootObject: dictionaryExample)
let dictionary: Dictionary? = NSKeyedUnarchiver.unarchiveObject(with: dataExample) as! [String : Any]
游樂場截圖
NSPropertyListSerialization 可能是另一種解決方案。
// Swift Dictionary To Data.
var data = try NSPropertyListSerialization.dataWithPropertyList(dictionaryExample, format: NSPropertyListFormat.BinaryFormat_v1_0, options: 0)
// Data to Swift Dictionary
var dicFromData = (try NSPropertyListSerialization.propertyListWithData(data, options: NSPropertyListReadOptions.Immutable, format: nil)) as! Dictionary<String, AnyObject>
斯威夫特 5
正如@yuyeqingshan 所說, PropertyListSerialization
是一個不錯的選擇
// Swift Dictionary To Data.
do {
let data = try PropertyListSerialization.data(fromPropertyList: [:], format: PropertyListSerialization.PropertyListFormat.binary, options: 0)
// do sth
} catch{
print(error)
}
// Data to Swift Dictionary
do {
let dicFromData = try PropertyListSerialization.propertyList(from: data, options: PropertyListSerialization.ReadOptions.mutableContainers, format: nil)
if let dict = dicFromData as? [String: Any]{
// do sth
}
} catch{
print(error)
}
對於 Swift 3:
let data = try PropertyListSerialization.data(fromPropertyList: authResponse, format: PropertyListSerialization.PropertyListFormat.binary, options: 0)
Leo 的回答給了我構建時間錯誤,因為NSData
與Data
。 函數unarchiveObject(with:)
接受一個Data
類型的變量,而函數unarchiveTopLevelObjectWithData()
接受一個NSData
類型的變量。
這是一個有效的Swift 3答案:
var names : NSDictionary = ["name":["John Smith"], "age": 35]
let namesData : NSData = NSKeyedArchiver.archivedData(withRootObject: names) as NSData
do{
let backToNames = try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(namesData) as! NSDictionary
print(backToNames)
}catch{
print("Unable to successfully convert NSData to NSDictionary")
}
對於 Swift 4 -
let dictionaryExample : [String:AnyObject] = ["user":"UserName" as AnyObject, "pass":"password" as AnyObject, "token":"0123456789" as AnyObject, "image":0 as AnyObject]
let dataExample : NSData = NSKeyedArchiver.archivedData(withRootObject: dictionaryExample) as NSData
let dictionary:NSDictionary? = NSKeyedUnarchiver.unarchiveObject(with: dataExample as Data)! as? NSDictionary
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.