我是一个快速学习的人。我使用swifthttp来获取要在我的控制台中显示的网络流,但它不起作用。我使用NSURLConnection.sendsynchronousrequest方法,它正常工作。现在,我想知道如何使用swiftHTTP获取网络流。
我的项目是CommandLine工具,我的main.swift中的代码如下
import Foundation
var request = HTTPTask()
request.GET("http://www.douban.com/j/app/radio/channels", parameters: nil, success: {(response: HTTPResponse) in
if response.responseObject != nil {
let data = response.responseObject as NSData
let str = NSString(data: data, encoding: NSUTF8StringEncoding)
println("response: \(str)") //prints the HTML of the page
}
},failure: {(error: NSError, response: HTTPResponse?) in
println("error: \(error)")
})
谢谢!
答案 0 :(得分:1)
请求在请求返回之前运行异步,主线程退出并且程序完成。添加
sleep(30)
关注您的代码。或者在iOS并发编程中投入一些时间(https://developer.apple.com/library/ios/documentation/General/Conceptual/ConcurrencyProgrammingGuide/Introduction/Introduction.html)。