swift - 等待来自异步完成处理程序的数据
问题描述
我不想使用调度队列或调度组来等待我的异步调用结束。所以我尝试使用完成处理程序但没有成功。我尝试打印内容或稍后使用此变量,但我的应用程序崩溃。谢谢
func initRequet() {
Helper().alomofireGetImage(URL: "...") { content in
self.profil = content.af_imageRoundedIntoCircle()
}
Helper().alomofireGet(URL: "...") { content in
self.content = content
print(content)
}
}
在我的文件助手中
func alomofireGet(URL: String, onCompletion: @escaping ((_ response: JSON) -> Void)) {
let header: HTTPHeaders = [
"user": "...",
"password": "..."
]
var contentJSON = JSON()
Alamofire.request(URL, headers: header).validate(statusCode: 200..<600).responseJSON() { (reponse) in
if reponse.result.isSuccess {
contentJSON = JSON(reponse.result.value!)
} else {
contentJSON = JSON(reponse.result.error!)
}
onCompletion(contentJSON)
}
}
解决方案
推荐阅读
- android - Android 通过 Intent Uri 检索图像失败:“无法访问内容......”
- amazon-web-services - Docker 服务自动停止
- algorithm - 在不使用第三个温度的情况下交换 2 个数字
- react-native - react-native如何获取视频的数据
- c++ - c++调用模板类特化成员函数
- python - 评估 json 文件 | 蟒蛇爬虫
- laravel - 在 Laravel 中上传多个文件
- integration - 将数据从 D365 写入 CDS 时出现双重写入错误
- assembly - ARM 程序集嵌套循环
- cmake - 避免在柯南配置文件和 CMake 之间重复配置