在Swift中使用未解析的标识符'parameters'

时间:2022-12-01 06:30:59

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

这里是获取CSRF令牌然后登录用户的代码

    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 个解决方案

#1


0  

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:

您在request-Block之外定义了参数。在您的请求块内部,您可以定义一个常量变量,该变量只能在该块内部访问。因此删除let,然后写:

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.

编辑:同样的问题是“令牌”,所以在这里做同样的事情。

#1


0  

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:

您在request-Block之外定义了参数。在您的请求块内部,您可以定义一个常量变量,该变量只能在该块内部访问。因此删除let,然后写:

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.

编辑:同样的问题是“令牌”,所以在这里做同样的事情。