2018-05-04 10:06:25 yingBi2014 阅读数 8829
let str:String = "意大利"
//字符串转Data
let data = str.data(using: String.Encoding.utf8)
//Data转字符串
let newStr = String(data: data!, encoding: String.Encoding.utf8)
print("data=" , data!  , "\n" ,  "newStr=" , newStr!)

输出结果

data= 9 bytes 
newStr= 意大利

2017-02-10 10:13:45 SuperMageHuang 阅读数 1474

1、NSDictionary转NSData
let data:NSData =NSKeyedArchiver.archivedData(withRootObject:dataDic) asNSData

2、NSData转NSDictionary
let dic =NSKeyedUnarchiver.unarchiveObject(with: dataas! 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>





2019-07-08 09:16:12 sun6223508 阅读数 257

1 字典转JSON字符串

        var body = [ "date": “dates”,"time": timestring,"repeat" ]
        let  data2 :Data! = try? JSONSerialization.data(withJSONObject: body, options: [])
        var  JSONString = String(data: data2, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue))
       

2 字典转Data

 

        var body = [ "date": dates,"time": timestring,"repeat": repeatstring,"remark": remarkstring, "scene_id":cuuentScene["id"]]
        let  data2 :Data! = try? JSONSerialization.data(withJSONObject: body, options: []) 
         

3 Data转JSON字符串

var body = [ "date": “dates”,"time": “timestring”, ]
        let  data2 :Data! = try? JSONSerialization.data(withJSONObject: body, options: [])
        var  JSONString = String(data: data2, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue))
        

4 JSON字符串转Data

var body = [ "date": dates,"time": timestring,"repeat": repeatstring,"remark": remarkstring, "scene_id":cuuentScene["id"]]
  let  data2 :Data! = try? JSONSerialization.data(withJSONObject: body, options: [])
 var  JSONString = String(data: data2, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue))
 var json=JSONString!.data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue))

 

2019-06-19 17:22:58 sun6223508 阅读数 593

var mBytes:[UInt8]  =  [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];

var data:Data = Data(bytes: mBytes, count: mBytes.count);

 

 

 

 

var bytes = [UInt8](data)

print(bytes)

2019-07-18 16:09:51 ForeverMyheart 阅读数 751

let image = UIImage.init(named: "bg_login")

//图片转data

  let data : Data = image!.pngData()!

//data转图片

let icon = UIImage.init(data: data)

 

       let  str = "测试"

//字符串转strTodata

        let strTodata = str.data(using: .utf8)

//data转字符串

        let dataToString = String(describing: String.init(data: strData!, encoding: .utf8))

 

 

   let imgUrl = URL.init(string: "http://img5.imgtn.bdimg.com/it/u=2380892543,1075553238&fm=26&gp=0.jpg")

 //url转data  

            let urlData = (imgUrl?.dataRepresentation)!

 

swift data 转byte

阅读数 1012

[UInt8],Data,String互转

博文 来自: xiongya8888
没有更多推荐了,返回首页