2018-05-04 10:06:25 yingBi2014 阅读数 8849
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-07-02 16:31:46 robinson_911 阅读数 3897


 /*
         打印json数据
         */
        LJDownLoadNetImage.request("GET", url: "http://jsonplaceholder.typicode.com/users/1") { (data, respond, error) in
            data转String
            let str = String(data:data!, encoding: String.Encoding.utf8)!
            print(str)

        }

打印结果

{
  "id": 1,
  "name": "Leanne Graham",
  "username": "Bret",
  "email": "Sincere@april.biz",
  "address": {
    "street": "Kulas Light",
    "suite": "Apt. 556",
    "city": "Gwenborough",
    "zipcode": "92998-3874",
    "geo": {
      "lat": "-37.3159",
      "lng": "81.1496"
    }
  },
  "phone": "1-770-736-8031 x56442",
  "website": "hildegard.org",
  "company": {
    "name": "Romaguera-Crona",
    "catchPhrase": "Multi-layered client-server neural-net",
    "bs": "harness real-time e-markets"
  }
}


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)

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

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-03-04 16:59:39 u011463507 阅读数 1019

var bytes = [UInt8](data2)

let pointer: UnsafeMutablePointer<UInt8> = UnsafeMutablePointer(&bytes)

 

swift Data 扩展

阅读数 259

[UInt8],Data,String互转

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