2016-07-01 58 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)") 
} 

如果你想要更多的解釋,只需發佈​​更多的代碼。