使用一个UIAlertController处理多个错误

时间:2017-05-29 13:12:58

标签: ios swift error-handling uialertcontroller urlsession

我有两个类,它们提供委托方法,在从API获取数据时报告各种错误。 HttpAPI获取天气数据,RadarAPI获取雷达图像。以下代码位于HomeViewController中,用于处理每个API的数据或错误。

// MARK: - HttpAPI Delegate

extension HomeViewController: HttpAPIDelegate {

    func apiSessionError(_ error: Error) {
        // show alert with error from url session
    }

    func apiHttpError(_ code: Int) {
        // show alert with HTTP error code
    }

    func apiJsonError(_ error: Error) {
        // show alert with json error
    }

    func apiSuccess(_ json: [String: Any]) {
        // update app with weather data
    }
}

// MARK: - RadarAPI Delegate

extension HomeViewController: RadarAPIDelegate {

    func radarSessionError(_ error: Error) {
        // show alert for url session error
    }

    func radarHttpError(_ code: Int) {
        // show alert with HTTP response error code
    }

    func radarImageError(_ error: String) {
        // show alert with image parsing error message
    }

    func radarSuccess(_ image: UIImage) {
        // update app with weather radar image
    }    
}

如果在获取天气数据或检索雷达图像时发生错误,我想显示警告。问题是如果HttpAPI发生错误,那么RadarAPI也会出错;因此,将提出两个UIAlertControllers,这是不推荐的。

如何处理多个错误消息并在单个UIAlertController中显示这些错误?

1 个答案:

答案 0 :(得分:0)

为什么不允许提交2 UIAlertControllers

无论如何,如果您不想显示2个不同的警报,请尝试在确定要显示的内容之前等待两个响应的机制。

相关问题