2016-07-01 6 views
1

私はSwiftに非常に新しく、天気予報アプリを作成しようとしています。私はプロトコルfunc weatherManagerFailedToLoadCityWithError(error: ErrorType)を持っています。 weatherManager.swiftで私は、このコードブロック天気予報アプリのエラータイププロトコル

func weatherManagerFailedToLoadCityWithError(error: ErrorType) { 

} 

任意の提案でweatherController.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) 
        }) 
       } 
      } 

がありますか?

あなたはこのようにそれを行うことができます

答えて

0

:あなたはあなたの関数でエラーを処理することができますしたい場合は

weatherManagerFailedToLoadCityWithError(error: NSError.serverNotFound()) 

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

そして、あなたの関数を呼び出す

func weatherManagerFailedToLoadCityWithError(error: ErrorType) { 
    print("Error description: \(error.userInfo. NSLocalizedDescriptionKey)") 
} 

詳細な説明が必要な場合は、コードをさらに投稿してください。

関連する問題