Skip to content

Instantly share code, notes, and snippets.

@vialyx
Created January 9, 2018 08:09
Show Gist options
  • Save vialyx/dfddb3e940bf96985af9fcb10a4d4598 to your computer and use it in GitHub Desktop.
Save vialyx/dfddb3e940bf96985af9fcb10a4d4598 to your computer and use it in GitHub Desktop.
import UIKit
var str = "Hello, playground"
class BaseViewController: UIViewController {
var handledErrorCodes: [Int: String] = [200: "localized.title", 210: "localized.title.error"]
func showError(error: NSError) {
let code = error.code
guard handledErrorCodes.keys.contains(code), let text = handledErrorCodes[code] as? String else {
print("\(self) can't handle this error code \(code)")
return
}
switch code {
case 200:
print("\(self) handle 200 code \(text)")
case 210:
print("\(self) handle 210 code \(text)")
default:
print("\(self) handle unknown code \(text)")
}
}
}
class RootViewController: BaseViewController {}
class ProfileViewController: BaseViewController {}
let error = NSError(domain: "cactussoft", code: 210, userInfo: nil)
let base = BaseViewController()
base.showError(error: error)
let root = RootViewController()
root.handledErrorCodes = [211: "localized.title", 220: "localized.title.error"]
root.showError(error: error)
let profile = ProfileViewController()
profile.handledErrorCodes = [310: "localized.title", 120: "localized.title.error"]
profile.showError(error: error)
let error211 = NSError(domain: "cactussoft", code: 211, userInfo: nil)
profile.showError(error: error211)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment