Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Http swift NSURLCONNECTION不工作_Http_Swift_Connection - Fatal编程技术网

Http swift NSURLCONNECTION不工作

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

我不熟悉swift。无法在http请求上获取以下代码。除了打印“开始…”,它不打印任何其他内容。所有的连接方法似乎都没有被调用?有什么建议吗

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

}