【发布时间】:2017-04-27 01:47:26
【问题描述】:
我正在使用 Alamofire 向 API 发出异步网络请求。在闭包内,我将响应的数据分配给特定变量(championRolesLibrary)。现在在闭包内,当我打印变量以查看其内容(项目 1)时,它包含我希望它拥有的所有数据。在闭包表达式(项目 2 )之外,当检查同一个对象并且在 1 和 2 之间没有以任何方式修改时,它不再有任何数据并且是空的。为什么是这样?
Alamofire.request(championRolesUrl!).responseJSON { response in
let result = response.result
if result.isSuccess {
if let data = result.value as? [[String:AnyObject]] {
for champion in data {
if let name = champion["name"] as? String {
if let roles = champion["roles"] as? Array<Dictionary<String, AnyObject>> {
championRoles = []
for role in roles {
if let roleName = role["name"] as? String {
championRoles.append(Role(roleName)!)
}
}
championRolesLibrary.append([name:championRoles])
}
}
}
print(championRolesLibrary) // 1
}
} else {
print("Downloading Role Data Failed Because: \(result.error)")
}
}
}
print(championRolesLibrary) // 2
【问题讨论】:
-
在两个打印语句上设置断点并注意它们的调用顺序。