NSDictionary to NSData and NSData to NSDictionary in Swift

前端 未结 6 1435
故里飘歌
故里飘歌 2020-12-12 23:54

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.

         


        
6条回答
  •  醉梦人生
    2020-12-13 00:25

    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
    

提交回复
热议问题