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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器