NSDictionary to NSData and NSData to NSDictionary

2019-01-21 12:47发布

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.

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.

Any help is greatly appreciated, thanks.

5条回答
霸刀☆藐视天下
2楼-- · 2019-01-21 13:20

NSPropertyListSerialization may be an alternative solution.

// 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>
查看更多
Fickle 薄情
3楼-- · 2019-01-21 13:20

For 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
查看更多
Animai°情兽
4楼-- · 2019-01-21 13:21

Leo's answer gave me build time errors because NSData isn't the same as Data. The function unarchiveObject(with:) takes a variable of type Data, whereas the function unarchiveTopLevelObjectWithData() takes a variable of type NSData.

This is a working Swift 3 answer:

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")
}
查看更多
叛逆
5楼-- · 2019-01-21 13:26

For Swift 3:

let data = try PropertyListSerialization.data(fromPropertyList: authResponse, format: PropertyListSerialization.PropertyListFormat.binary, options: 0)
查看更多
戒情不戒烟
6楼-- · 2019-01-21 13:27

You can use NSKeyedArchiver and NSKeyedUnarchiver

Example for 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

let dataExample: Data = NSKeyedArchiver.archivedData(withRootObject: dictionaryExample)
let dictionary: Dictionary? = NSKeyedUnarchiver.unarchiveObject(with: dataExample) as! [String : Any]

Screenshot of playground

enter image description here

查看更多
登录 后发表回答