NSMutableURLRequest和Argument标签错误

时间:2017-05-24 11:34:10

标签: ios swift

我收到的错误是"参数标签'(网址:,cachePolicy:,timeoutInterval:)'不匹配任何可用的重载"我不知道怎么解决这个有人知道吗?我使用的是Swift 3.0和XCode 8.3.2 Here is my code

import Foundation
import UIKit
extension UIViewController {

    func makeImageViewWithColor(_ color: UIColor) -> UIImage {
        let rect: CGRect = CGRect (x: 0, y: 0, width: 1, height: 1)
        color.setFill()
        UIRectFill(rect)
        let image:UIImage =UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        return image
    }
    //Function Alert View
    func alertView(title:String, message:String, alertStyle:UIAlertControllerStyle, alertAction:UIAlertAction) {
        let alert:UIAlertController = UIAlertController(title: title, message: message, preferredStyle: alertStyle)
        alert.addAction(alertAction)
        self.present(alert, animated: true, completion: nil)
    }


    //Function query to server

    func queryToServer(urlSource:String, postString:String, result:@escaping (_ dataResult:NSData?, _ errorResult:NSError) -> ()) {
        let url:NSURL = NSURL(string: urlSource)!
        let request = NSMutableURLRequest (URL: url, cachePolicy: NSURLRequest.CachePolicy.reloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 10)
        request.HTTPMethod = "POST"
        request.HTTPBody = glGetString.dataUsingEncoding(NSUTF8StringEncoding)
        let session = URLSession.shared
        session.dataTaskWithRequest(request) { (data, response, error) -> Void in
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                if error == nil {
                    result(dataResult: data, errorResult: nil)
                } else {
                    result(dataResult: nil, errorResult: error)
                }
            })
        }
    }

}

3 个答案:

答案 0 :(得分:1)

根本不要在Swift 3中使用NSMutableURLRequest

使用Swift 3原生语法,有新的结构URLURLRequest

var关键字使请求变得可变。

let url = URL(string: urlSource)!
var request = URLRequest(url: url, cachePolicy: .reloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 10)

PS:代码中的问题是大写的URL参数标签。

编辑:方法queryToServer的Swift 3等价物是:

func queryToServer(urlSource:String, postString:String, result:@escaping (Data?, Error?) -> ()) {
    let url = URL(string: urlSource)!
    var request = URLRequest (url: url, cachePolicy: .reloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 10)
    request.httpMethod = "POST"
    request.httpBody = postString.data(using: .utf8)
    let session = URLSession.shared
    session.dataTask(with: request) { (data, response, error) in
        DispatchQueue.main.async {
            result(data, error)
        }
    }.resume()
}

调度块中的错误检查是无用的。数据有效而不是错误nil,反之亦然。另一方面,您必须resume启动数据任务,glGetString可能是postString

答案 1 :(得分:0)

swift 3.0必须使用参数标签

    public init(url URL: URL, cachePolicy: NSURLRequest.CachePolicy, timeoutInterval: TimeInterval)

在上述方法中,Aurgument标签是

  1. URL
  2. 的CachePolicy
  3. timeoutInterval
  4. 请提供,您的第一个参数为" 网址"不是" 网址"

答案 2 :(得分:0)

NSURL更改为URL,并在函数参数中设置大写URL为小写字母

let url = URL(string: urlSource)!
let request = NSMutableURLRequest(url: url, cachePolicy: .reloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 10)
request.httpMethod = "POST"