Error handling 天气应用程序中的ErrorType协议
我对Swift非常陌生,正在尝试创建天气应用程序。我有协议Error handling 天气应用程序中的ErrorType协议,error-handling,swift2,swift-protocols,Error Handling,Swift2,Swift Protocols,我对Swift非常陌生,正在尝试创建天气应用程序。我有协议func weatherManagerFailedToLoadCityWithError(错误:ErrorType)。在weatherManager.swift有一些代表 } else if status == 404 { // City not found if self.delegate != nil { dispatch_asyn
func weatherManagerFailedToLoadCityWithError(错误:ErrorType)
。在weatherManager.swift有一些代表
} else if status == 404 {
// City not found
if self.delegate != nil {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.delegate?.weatherManagerFailerToLoadCityWithError(.InvalidResponse)
})
}
} else {
// Some other here?
if self.delegate != nil {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.delegate?.weatherManagerFailerToLoadCityWithError(.MissingData)
})
}
}
在这个代码块中输入weatherController.swift该怎么办
func weatherManagerFailedToLoadCityWithError(error: ErrorType) {
}
有什么建议吗?你可以这样做:
private struct ErrorInformation {
static let Domain = "us.firmaName"
static let ServerNotFoundDomain = "\(ErrorInformation.Domain).notFound"
}
private extension NSError {
static func serverNotFound() -> NSError {
let userInfo = [
NSLocalizedDescriptionKey: NSLocalizedString("Server Not Found", comment: ""),
NSLocalizedFailureReasonErrorKey: NSLocalizedString("The Server you are asking for is not available.", comment: ""),
NSLocalizedRecoverySuggestionErrorKey: NSLocalizedString("Please proof your URL bla-bla-bla.", comment: "")
]
return NSError(domain: ErrorInformation.ServerNotFoundDomain, code: 404, userInfo: userInfo)
}
然后调用您的函数:
weatherManagerFailedToLoadCityWithError(error: NSError.serverNotFound())
func weatherManagerFailedToLoadCityWithError(error: ErrorType) {
print("Error description: \(error.userInfo. NSLocalizedDescriptionKey)")
}
如果需要,可以处理函数中的错误:
weatherManagerFailedToLoadCityWithError(error: NSError.serverNotFound())
func weatherManagerFailedToLoadCityWithError(error: ErrorType) {
print("Error description: \(error.userInfo. NSLocalizedDescriptionKey)")
}
如果您需要更多的解释,只需发布更多的代码 您可以这样做:
private struct ErrorInformation {
static let Domain = "us.firmaName"
static let ServerNotFoundDomain = "\(ErrorInformation.Domain).notFound"
}
private extension NSError {
static func serverNotFound() -> NSError {
let userInfo = [
NSLocalizedDescriptionKey: NSLocalizedString("Server Not Found", comment: ""),
NSLocalizedFailureReasonErrorKey: NSLocalizedString("The Server you are asking for is not available.", comment: ""),
NSLocalizedRecoverySuggestionErrorKey: NSLocalizedString("Please proof your URL bla-bla-bla.", comment: "")
]
return NSError(domain: ErrorInformation.ServerNotFoundDomain, code: 404, userInfo: userInfo)
}
然后调用您的函数:
weatherManagerFailedToLoadCityWithError(error: NSError.serverNotFound())
func weatherManagerFailedToLoadCityWithError(error: ErrorType) {
print("Error description: \(error.userInfo. NSLocalizedDescriptionKey)")
}
如果需要,可以处理函数中的错误:
weatherManagerFailedToLoadCityWithError(error: NSError.serverNotFound())
func weatherManagerFailedToLoadCityWithError(error: ErrorType) {
print("Error description: \(error.userInfo. NSLocalizedDescriptionKey)")
}
如果您需要更多的解释,只需发布更多的代码