如何在Alamofire中保存对NSDictionary的响应价值?

时间:2023-01-09 19:14:34

I'm using Alamofire v3.4.1 and i'm getting result of JSON via GET method like this,

我正在使用Alamofire v3.4.1,我通过这样的GET方法得到JSON的结果,

@IBAction func verifyAction(sender: AnyObject) { 

    let tempEmail: String = "\(emailTxtField.text!)"
    let tempApi : String = appDelegate.apiKey as String 
    var status : String
    Alamofire.request(.GET, "http://www.somejsonurl.com/checkEmailExits", parameters: ["APIKey": "\(tempApi)","Email" : "\(tempEmail)"])
            .responseJSON { response in
              print(response.request)  // original URL request
              print(response.response) // URL response
              print(response.data)     // server data
              print(response.result)   // result of response serialization

                let JSON = response.result.value         
                let dict1 = JSON!["Result"]
                let dict2 = dict1!!["Status"]
                status = dict2 as! String

    }

Now as soon as i assign value to status xcode gives me error:

现在,只要我为状态xcode赋值,就会给出错误:

Variable 'status' captured by a closure before being initialized

初始化之前由闭包捕获的变量“status”

Is there a way to store response to NSDictionary that can be used in other functions too ? Thanks

有没有办法存储可以在其他函数中使用的NSDictionary响应?谢谢

1 个解决方案

#1


2  

You need to initialize your object before assigning value to it so declare your status object like this

您需要在为其赋值之前初始化对象,因此请像这样声明您的状态对象

var status = String()

Or

要么

var status: String = ""

#1


2  

You need to initialize your object before assigning value to it so declare your status object like this

您需要在为其赋值之前初始化对象,因此请像这样声明您的状态对象

var status = String()

Or

要么

var status: String = ""