dpw5865 2014-11-22 19:34
浏览 74

在Swift中使用未解析的标识符“参数”

So I get this error when trying to run my script

Use of unresolved identifier 'parameters'

And here's the code to get a CSRF token then log the user in

    var username = usernameField.text
    var password = passwordField.text

    var token = String()
    var parameters = String?()

    //Get CSRF token
    Alamofire.request(.GET, "https://localhost/api/csrf")
        .responseJSON{(request, response, data, error) in
            var jsonParse = data as NSDictionary
            var responseJSON = JSON(jsonParse)

            if(responseJSON["success"]) == false{
                println(responseJSON["error"])
            }
            else{
                let token = responseJSON["data"].string

                let parameters = [
                    "user": username,
                    "password": password,
                    "csrf": token
                ]
            }

    }

    Alamofire.request(.POST, "https://localhost/api/login", parameters: parameters)
        .responseJSON{(request, response, JSON, error) in
            println(token)
            println(JSON)

    }

I don't see why I can't access parameters out of the scope considering that I already initiated it like so

var parameters = String?()

Any ideas?

  • 写回答

1条回答 默认 最新

  • dongtan7639 2014-11-22 19:49
    关注

    You defined your parameters outside your request-Block. Inside your request-Block you define a constant variable, which is only accessible inside that block. Therefore remove that let and just write:

    parameters = [
                    "user": username,
                    "password": password,
                    "csrf": token
                ]
    

    And I think the second request should be made after you get the response of the first request? If so, put the second request inside the callback of your first request.

    EDIT: the same problem goes for "token", so do the same here.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog