Http swift NSURLCONNECTION不工作
我不熟悉swift。无法在http请求上获取以下代码。除了打印“开始…”,它不打印任何其他内容。所有的连接方法似乎都没有被调用?有什么建议吗Http swift NSURLCONNECTION不工作,http,swift,connection,Http,Swift,Connection,我不熟悉swift。无法在http请求上获取以下代码。除了打印“开始…”,它不打印任何其他内容。所有的连接方法似乎都没有被调用?有什么建议吗 class Network { var data: NSMutableData = NSMutableData() func getAcctSummary() { let urlAcctSummary = "http://www.google.com" var url: NSURL = NSURL(stri
class Network {
var data: NSMutableData = NSMutableData()
func getAcctSummary() {
let urlAcctSummary = "http://www.google.com"
var url: NSURL = NSURL(string: urlAcctSummary)
var request: NSURLRequest = NSURLRequest(URL: url)
var connection: NSURLConnection = NSURLConnection(request: request, delegate: self, startImmediately: false)
connection.start()
println("started...")
}//getAcctSummary
func connection(connection: NSURLConnection!, didFailWithError error: NSError!) {
println("Failed with error:\(error.localizedDescription)")
}
func connection(didReceiveResponse: NSURLConnection!, didReceiveResponse response: NSURLResponse!) {
println("didReceiveResponse")
}
func connection(connection: NSURLConnection!, didReceiveData conData: NSData!) {
self.data.appendData(conData)
println("here 1")
}
func connectionDidFinishLoading(connection: NSURLConnection!) {
println(self.data)
println("here")
}
}
为了确定支持哪些可选协议方法,NSURLConnection委托必须是NSObject的子类。将第一行更改为:
class Network : NSObject {
你应该很乐意去尝试使用swift-nsursession。最适合我的是一些前代码,但我不得不拿出我的自定义代码
func someFunc() ->()
{
var session = NSURLSession.sharedSession()
var personURL = "URL"
let url = NSURL(string:personURL)
var task:NSURLSessionDataTask = session.dataTaskWithURL(url, completionHandler:apiHandler)
task.resume()
}
func apiHandler(data:NSData!, response:NSURLResponse!, error:NSError!)
{
if error
{
println("API error: \(error), \(error.userInfo)")
}
var jsonError:NSError?
var json:JMSMediaDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &jsonError) as Dictionary
println(json)
//do stuff with data
}