[英]NSDictionary to NSData and NSData to NSDictionary in Swift
I am not sure if I am using the dictionary or the data object or both incorrectly.我不确定我是否错误地使用了字典或数据对象或两者。 I m trying to get used to the switch to swift but I'm having a little trouble.
我正在尝试习惯切换到 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
I need the NSDictionary
to encode to an NSData
object as well as taking that NSData
object and decode it into a NSDictionary
.我需要
NSDictionary
编码为NSData
对象以及获取该NSData
对象并将其解码为NSDictionary
。
Any help is greatly appreciated, thanks.非常感谢任何帮助,谢谢。
You can use NSKeyedArchiver
and NSKeyedUnarchiver
您可以使用
NSKeyedArchiver
和NSKeyedUnarchiver
Example for swift 2.0+ 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
Swift3.0斯威夫特3.0
let dataExample: Data = NSKeyedArchiver.archivedData(withRootObject: dictionaryExample)
let dictionary: Dictionary? = NSKeyedUnarchiver.unarchiveObject(with: dataExample) as! [String : Any]
Screenshot of playground游乐场截图
NSPropertyListSerialization may be an alternative solution. 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>
Swift 5斯威夫特 5
as @yuyeqingshan said, PropertyListSerialization
is a good option正如@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's answer gave me build time errors because NSData
isn't the same as Data
. Leo 的回答给了我构建时间错误,因为
NSData
与Data
。 The function unarchiveObject(with:)
takes a variable of type Data
, whereas the function unarchiveTopLevelObjectWithData()
takes a variable of type NSData
.函数
unarchiveObject(with:)
接受一个Data
类型的变量,而函数unarchiveTopLevelObjectWithData()
接受一个NSData
类型的变量。
This is a working Swift 3 answer:这是一个有效的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")
}
For Swift 4 -对于 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.