2016-07-01 23 views
1

我对Swift非常陌生,试图创建天气应用程序。我有协议func weatherManagerFailedToLoadCityWithError(error: ErrorType)。在weatherManager.swift有一些代表天气应用程序中的ErrorType协议

} 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) 
        }) 
       } 
      } 

我应该做我在这个代码块

func weatherManagerFailedToLoadCityWithError(error: ErrorType) { 

} 

任何建议weatherController.swift什么?

回答

0

你可以这样说:

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)") 
} 

如果你想要更多的解释,只需发布​​更多的代码。