【问题标题】:Error while handling "NSURLConnection.sendAsynchronousRequest"处理“NSURLConnection.sendAsynchronousRequest”时出错
【发布时间】:2017-04-26 11:50:33
【问题描述】:

当我执行NSURLConnection.sendAsynchronousRequest 时,它总是显示相同的错误:

从'(NSURLResponse?, NSData?, NSError?) throws -> Void' 类型的抛出函数到非抛出函数类型'(NSURLResponse?, NSData?, NSError?) -> Void' 的无效转换”

这是代码:

    progressBar.hidden = false
    progressBar.progress = 0
    var am = Int(ammount * 100)
    var amToOwner = Int()
    let process = Float(100-fee)*Float(ammount)/Float(100)
    amToOwner = Int(self.roundToPlaces(0, number: process))
    progressBar.progress = 33.3
    let urlPath: String = "https://api.stripe.com/v1/charges"
    let url: NSURL = NSURL(string: urlPath)!
    let session = NSURLSession.sharedSession()
    let request1: NSMutableURLRequest = NSMutableURLRequest(URL: url)

    request1.HTTPMethod = "POST"
    var stringPost = String()
    stringPost="amount=\(am)&currency=eur&source=" + self.tok + "&description=\"\(description)\"&destination[account]=\(ownerAccountID!)&destination[amount]=\(try! Int(amToOwner*100))" // Key and Value
    stringPost = stringPost.stringByReplacingOccurrencesOfString("\\", withString: "")
    print(stringPost)

    let data = stringPost.dataUsingEncoding(NSUTF8StringEncoding)

    request1.timeoutInterval = 60
    request1.HTTPBody=data
    request1.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
    request1.setValue("Bearer " + _secretKey, forHTTPHeaderField: "Authorization")
    request1.HTTPShouldHandleCookies=false
    print(request1.allHTTPHeaderFields)
    NSURLConnection.sendAsynchronousRequest(request1, queue: queue, completionHandler:{ (response: NSURLResponse?, data: NSData?, error: NSError?) -> Void in)
       self.progressBar.progress = 66.6
        self.tok = ""
        do {
            self.progressBar.progress = 100
            if let jsonResult = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? NSDictionary {
                print("ASynchronous\(jsonResult)")
                if jsonResult.valueForKey("error") != nil{
                    let alert = UIAlertController()
                    alert.title = "Error"
                    let callFunction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Cancel, handler: nil)
                    alert.addAction(callFunction)
                    alert.message = String(jsonResult.valueForKey("error")!.valueForKey("message")!)
                    self.presentViewController(alert, animated: true, completion: nil)
                } else {
                    let alert = UIAlertController()
                    let callFunction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Cancel, handler: nil)
                    alert.addAction(callFunction)
                    alert.title = jsonResult.valueForKey("outcome")!.valueForKey("seller_message")! as! String
                    var amm = jsonResult.valueForKey("amount")!.floatValue / 100
                    alert.message = "Ammount: \(amm) \(jsonResult.valueForKey("currency")!)\n Description: \(jsonResult.valueForKey("description")!)"
                    self.presentViewController(alert, animated: true, completion: nil)
                    self.progressBar.hidden = true
                }
            }
        } catch let error as NSError {
            self.progressBar.progress = 100
            print(error.localizedDescription)
            self.progressBar.hidden = true
        }


    })

什么错误?你能帮帮我吗?

我正在使用 XCode 7.3.1

【问题讨论】:

  • 您复制了错误,但由于没有任何降价指示,因此都没有显示为错误。我消除了重复并添加了降价以使其看起来像“错误文本”。

标签: swift


【解决方案1】:

我找到了解决办法:

NSURLConnection.sendAsynchronousRequest(request1, queue: queue, completionHandler:{ (response: NSURLResponse?, data: NSData?, error: NSError?) -> Void in
        self.progressBar.progress = 66.6
        self.tok = ""
            self.progressBar.progress = 100
            if let jsonResult = try? NSJSONSerialization.JSONObjectWithData(data!, options: []) as? NSDictionary {
                print("ASynchronous\(jsonResult)")
                if jsonResult!.valueForKey("error") != nil{
                    let alert = UIAlertController()
                    alert.title = "Error"
                    let callFunction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Cancel, handler: nil)
                    alert.addAction(callFunction)
                    alert.message = String(jsonResult!.valueForKey("error")!.valueForKey("message")!)
                    self.presentViewController(alert, animated: true, completion: nil)
                } else {
                    let alert = UIAlertController()
                    let callFunction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Cancel, handler: nil)
                    alert.addAction(callFunction)
                    alert.title = jsonResult!.valueForKey("outcome")!.valueForKey("seller_message")! as! String
                    var amm = jsonResult!.valueForKey("amount")!.floatValue / 100
                    alert.message = "Ammount: \(amm) \(jsonResult!.valueForKey("currency")!)\n Description: \(jsonResult!.valueForKey("description")!)"
                    self.presentViewController(alert, animated: true, completion: nil)
                    self.progressBar.hidden = true
                }
        }
    })

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多