0

我正在尝试通过以下参数快速发出获取请求

var dict = [
    "id_struttura" : 2,
    "prenCheck" : [
        "codice" : "14:30_15:20_1_3831_0",
        "id_sport" : 6,
        "ora_fine" : "20/02/2015 15:20:00",
        "ora_inizio" : "20/02/2015 14:30:00",
        "rec" : 0,
        "soci" : [
            [
                "esterno" : 0,
                "id" : "1980"
            ],
            [
                "esterno" : 0,
                "id" : "51"
            ]
        ]
    ]
]

使用以下方法:

private func genericRequest(method: Methods, url: String, data: [String: AnyObject]?) -> AnyObject?{
    var getRequest : NSURLRequest?
    var postRequest : NSMutableURLRequest?

    if method == Methods.GET{
        var completeUrl = url

        if(data != nil){
            //println(data)
            completeUrl += "?"
            for (key, value) in data! {

                completeUrl += "\(key)=\(value)&"
            }
            completeUrl = completeUrl.substringToIndex(completeUrl.endIndex.predecessor())
        }
        //println(completeUrl)

        let url = NSURL(string: completeUrl)
        getRequest = NSURLRequest(URL: url!)

    }else if method == Methods.POST{
        postRequest = NSMutableURLRequest(URL: NSURL(string: url)!)
        var session = NSURLSession.sharedSession()
        postRequest!.HTTPMethod = "POST"

        var params : String = ""

        for (key, value) in data! {

            params += "\(key)=\(value)&"
        }
        params = params.substringToIndex(params.endIndex.predecessor())

        var err: NSError?
        postRequest!.HTTPBody = params.dataUsingEncoding(NSUTF8StringEncoding);
    }

    var response: NSURLResponse? = nil
    var error: NSError? = nil
    var request = getRequest != nil ? getRequest! : postRequest!
    let reply = NSURLConnection.sendSynchronousRequest(request, returningResponse:&response, error:&error)

    //println((response as NSHTTPURLResponse).statusCode)
    let results = NSString(data:reply!, encoding:NSUTF8StringEncoding)
    var json: AnyObject? = convertDataToJSON(reply)
    return json
}

将其称为(因为 Method 只是一个枚举)它在发出 get 请求时genericRequest(Method.GET ,url:url, data: dict)无法解开 data 变量,但在发出 post 请求时可以。for (key,value)in data!由于我只能使用获取此任务的请求(我正在处理一个糟糕的休息界面),有人可以帮助我吗?

更新:我也试过了if let data = data { ...,它在这条线上崩溃了 unexpectedly found nil while unwrapping an Optional value

4

2 回答 2

0

为什么您不像在 GET 方法中那样检查 POST 方法中的数据是否为零。我认为 post 方法是您尝试解开 nil 的地方

if method == Methods.GET{
    var completeUrl = url

    // here you check if data is nil
    if(data != nil){
        //println(data)
        completeUrl += "?"
        for (key, value) in data! {

            completeUrl += "\(key)=\(value)&"
        }
        completeUrl = completeUrl.substringToIndex(completeUrl.endIndex.predecessor())
    }
    //println(completeUrl)

    let url = NSURL(string: completeUrl)
    getRequest = NSURLRequest(URL: url!)

}else if method == Methods.POST{
    postRequest = NSMutableURLRequest(URL: NSURL(string: url)!)
    var session = NSURLSession.sharedSession()
    postRequest!.HTTPMethod = "POST"

    var params : String = ""
    //check here as well
    if data != nil{
        for (key, value) in data! {

            params += "\(key)=\(value)&"
        }
        params = params.substringToIndex(params.endIndex.predecessor())
    }

    var err: NSError?
    postRequest!.HTTPBody = params.dataUsingEncoding(NSUTF8StringEncoding);
}
于 2015-02-25T11:25:26.510 回答
0

您正在使用大量的力展开 ( !)。您执行此操作的每个地方都是您向编译器承诺可选项永远不会为零的地方。在大多数地方,我认为您应该允许编译器通过使用其他处理可选项的方法(可选链接、nil 合并、映射等)来证明 nil 的安全性。

在您的具体情况下,我不知道您为什么不这样做:

if let data = data {

代替:

if (data != nil) {

然后不得不用!.

如果您想了解有关如何不强制展开的更多信息,我最近在博客讨论了在 Swift 中处理选项的工具。

于 2015-02-25T11:05:33.177 回答