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 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据