Swift同步下载数据

戴维营教育代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//HTTP/HTTPS
        //URL: [url]http://pic.baike.soso.com/p/20120424/bki-20120424022918-1539307851.jpg[/url]
         
        //创建一个URL对象
        var url = NSURL(string: "/upload/otherpic73/203912.jpg")
        //创建一个HTTP请求
        var request = NSURLRequest(URL: url)
        //发送同步请求,开始下载数据
         
        var response: NSURLResponse?
        var error: NSError?
        var data = NSURLConnection.sendSynchronousRequest(request, returningResponse: &response, error: &error)
        println("\(response):\(error)")
         
        var httpResponse = response as NSHTTPURLResponse
        //数据下载完成
        if httpResponse.statusCode == 200 {
            println("下载了\(data.length)字节")
            data.writeToFile("/Users/cheetah/Desktop/test.jpg", atomically: true)
        }
        else {
            println("下载失败: \(httpResponse.statusCode)")
        }

网页题目:Swift同步下载数据
文章出自:http://pwwzsj.com/article/pijeeg.html