@@ -52,10 +52,10 @@ class ViewController: UIViewController {
5252 requestLabel. numberOfLines = 6
5353 self . view. addSubview ( requestLabel)
5454
55- let responseLable = UILabel ( frame: CGRect ( x: 20 , y: 130 , width: 400 , height: 600 ) )
56- responseLable . text = " request... "
57- responseLable . numberOfLines = 100
58- self . view. addSubview ( responseLable )
55+ let responseLabel = UILabel ( frame: CGRect ( x: 20 , y: 130 , width: 400 , height: 600 ) )
56+ responseLabel . text = " request... "
57+ responseLabel . numberOfLines = 100
58+ self . view. addSubview ( responseLabel )
5959
6060 //生成UI >>>>>>>>>>>>>>>>>>>>>
6161
@@ -65,23 +65,23 @@ class ViewController: UIViewController {
6565
6666 //请求URL
6767
68- let request : NSMutableURLRequest = NSMutableURLRequest ( URL : NSURL ( string: url) !)
68+ let request = NSMutableURLRequest ( url : URL ( string: url) !)
6969
70- request. HTTPMethod = " POST "
70+ request. httpMethod = " POST "
7171 //设置发送的数据格式为JSON
7272 request. setValue ( " application/json " , forHTTPHeaderField: " Content-Type " )
7373 do {
74- request. HTTPBody = try NSJSONSerialization . dataWithJSONObject ( json, options: NSJSONWritingOptions . PrettyPrinted )
74+ request. httpBody = try JSONSerialization . data ( withJSONObject : json, options: JSONSerialization . WritingOptions . prettyPrinted )
7575 } catch {
7676 print ( " Something went wrong! " )
7777 }
7878
7979 //默认session配置
80- let config = NSURLSessionConfiguration . defaultSessionConfiguration ( )
81- let session = NSURLSession ( configuration: config)
80+ let config = URLSessionConfiguration . default
81+ let session = URLSession ( configuration: config)
8282
8383 //发起请求
84- let dataTask = session. dataTaskWithRequest ( request) { ( data, response, error) in
84+ let dataTask = session. dataTask ( with : request as URLRequest ) { ( data, response, error) in
8585
8686 print ( " \n \n received result! \n \n " )
8787
@@ -90,30 +90,32 @@ class ViewController: UIViewController {
9090 print ( error)
9191
9292 //数据类型转换
93- let jsonData : NSDictionary = try ! NSJSONSerialization . JSONObjectWithData ( data!, options: . MutableContainers ) as! NSDictionary
93+ let jsonData : NSDictionary = try ! JSONSerialization . jsonObject ( with : data!, options: . mutableContainers ) as! NSDictionary
9494
9595 print ( jsonData)
9696
97- let res : String = self . toJSONString ( jsonData) ;
97+ let res : String = self . toJSONString ( jsonData as! [ String : Any ] ) ;
9898 print ( " Response = \n " + res)
9999
100100
101101 //显示返回结果
102- dispatch_async ( dispatch_get_main_queue ( ) , {
102+ DispatchQueue . main . async {
103103
104- responseLable . text = " Response: \n " + res
104+ responseLabel . text = " Response: \n " + res
105105 print ( " set text end \n \n " )
106- } ) ;
106+ }
107107
108108 }
109109
110110 //请求开始
111111 dataTask. resume ( )
112112 }
113113
114- func toJSONString( jsonData: NSDictionary ! ) -> String {
115- let data : NSData ! = try ? NSJSONSerialization . dataWithJSONObject ( jsonData, options: [ NSJSONWritingOptions . PrettyPrinted] ) as NSData !
116- let str = String ( data: data, encoding: NSUTF8StringEncoding)
114+ func toJSONString( _ jsonData: [ String : Any ] ! ) -> String {
115+ guard let data = try ? JSONSerialization . data ( withJSONObject: jsonData!, options: [ JSONSerialization . WritingOptions. prettyPrinted] ) else {
116+ return " "
117+ }
118+ let str = String ( data: data, encoding: String . Encoding. utf8)
117119 return str!
118120 }
119121
0 commit comments