Skip to content

Instantly share code, notes, and snippets.

@unnamedd
Last active March 29, 2019 23:51
Show Gist options
  • Save unnamedd/ac5995902b113208c5213c3dc0f1ca2c to your computer and use it in GitHub Desktop.
Save unnamedd/ac5995902b113208c5213c3dc0f1ca2c to your computer and use it in GitHub Desktop.
How to use the right methods to right types
// Common - Portuguese - pt-BR.lproj/Localizable.strings
// "common.change" = "Mudar";
// "common.error" = "Erro";
// "common.delete" = "Exluir";
// "common.wait" = "Aguarde";
// Common - English - en.lproj/Localizable.strings
// "common.change" = "Change";
// "common.error" = "Error";
// "common.delete" = "Delete";
// "common.wait" = "Wait";
// MARK: - Project Constants
enum Project {
// Image Names
enum Images {
enum Checkout: String, ImageRepresentable {
case first = "icon-first-image"
case second = "icon-second-image"
}
}
// Localizable Strings
enum Localizable {
// Common
enum Common: String, LocalizeRepresentable {
case change = "common.change"
case error = "common.error"
case delete = "common.delete"
case wait = "common.wait"
}
}
}
// MARK: - Representable Protocols
protocol ImageRepresentable: RawRepresentable {
var image: UIImage? { get }
}
protocol LocalizeRepresentable: RawRepresentable {
var localized: String { get }
}
// MARK: - Representable Protocols Extensions
extension ImageRepresentable where RawValue == String {
var image: UIImage? {
return UIImage(named: rawValue)
}
}
extension LocalizeRepresentable where RawValue == String {
var localized: String {
return NSLocalizedString(rawValue, comment: "")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment