【发布时间】:2026-01-22 13:50:02
【问题描述】:
我刚刚完成了从 Alamofire 4 到 5 的升级。除了记录调试响应之外,一切都很好。在 Alamofire 4 中,您可以访问响应数据。
NotificationCenter.default.addObserver(forName: NSNotification.Name.Task.DidComplete, object: nil, queue: OperationQueue.main) { notification in
if let responseData = notification.userInfo?[Notification.Key.ResponseData] as? Data {
if responseData.count > 0 {
let body = String(decoding: responseData, as: UTF8.self)
print("Response Body: \(body)")
}
}
}
在 Almaofire 5 中,您似乎无权访问响应数据。 userInfo 中唯一的内容是通过 notification.request 访问的 Alamofire.Request。
NotificationCenter.default.addObserver(forName: Request.didCompleteTaskNotification, object: nil, queue: OperationQueue.main) { notification in
// no response data here
}
有人知道如何访问响应数据吗?
【问题讨论】: