美文网首页swift 基础语法
swift 开发 - NSDictionary与NSData互转

swift 开发 - NSDictionary与NSData互转

作者: 谌文 | 来源:发表于2019-06-18 19:11 被阅读0次

    1、NSDictionary转NSData

     let data: NSData = NSKeyedArchiver.archivedData(withRootObject:dataDic) as? NSData ?? NSData()
    

    2、NSData转NSDictionary

    let dic = NSKeyedUnarchiver.unarchiveObject(with: data as? Data)as!NSDictionary
    

    3、NSDictionary转Data

    let date = try? JSONSerialization.data(withJSONObject: dic, options: .prettyPrinted)
    

    4、Data转Dictionary

    let json = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers)as! Dictionary<String,String>
    

    相关文章

      网友评论

        本文标题:swift 开发 - NSDictionary与NSData互转

        本文链接:https://www.haomeiwen.com/subject/gpkqqctx.html