我刚刚开始“Alamofire”进行JSON解析。现在,我面临一些问题,如下所示:
问题陈述:无法访问Alamofire块外部的数据。
编码资料:
import UIKit
import Alamofire
class ViewController: UIViewController
{
var dataValue = String()
override func viewDidLoad()
{
super.viewDidLoad()
Alamofire.request("url") .responseJSON
{ response in
dataValue = response.result.value
print(dataValue) // It prints value
}
print(dataValue) //It does not print any thing or nil.
}
}
最佳答案
var dataValue = String()
override func viewDidLoad()
{
super.viewDidLoad()
Alamofire.request("url") .responseJSON
{ response in
dataValue = response.result.value
self.myFunction(str: dataValue)
}
}
func myFunction(str: String)
{
print("str value ====%@",str)
}