NSDictionary to NSData and NSData to NSDictionary in Swift

前端 未结 6 1465
故里飘歌
故里飘歌 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

    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
    

提交回复
热议问题