This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum Icon { | |
case alert | |
case car | |
case person | |
... | |
} | |
extension Icon { | |
var stringValue: String? { | |
let items = JSONDecoder().decode([IconDescription], from: json) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[{ | |
"name": "alert", | |
"char": "🔔" | |
}, | |
{ | |
"name": "car", | |
"char": "🚘" | |
}] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class IconView: UIView { | |
/// Change the icon being displayed | |
var icon: Icon { | |
didSet { | |
label.text = icon.rawValue | |
} | |
} | |
/// Change the color of the icon being displayed | |
var color: UIColor { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Icon { | |
func callAsFunction(color: UIColor = .black) -> IconView { | |
IconView(icon: self, color: color) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class IconView: UIView { | |
var icon: Icon | |
var color: UIColor | |
private let label: UILabel = { | |
let font = UIFont.iconFont(ofSize: 500) | |
let label = UILabel() | |
label.font = font | |
label.adjustsFontSizeToFitWidth = true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum Icon: String, CaseIterable { | |
case bag = "\u{e807}" | |
case bank = "\u{e808}" | |
case batteryout = "\u{e809}" | |
case battery = "\u{e80a}" | |
case bike = "\u{e80b}" | |
case bulb = "\u{e80c}" | |
case calendar = "\u{e80d}" | |
... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// { | |
// "name": "Janine", | |
// "color": "GREEN" | |
// } | |
let data = requestData() | |
let decoder = JSONDecoder() | |
let object = try? decoder.decode(User.self, from: data) | |
print(object) // User(name: "Janine", color: .unknown) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct User: Codable { | |
let name: String | |
let color: Color | |
enum Color: String, Codable, UnknownCase { | |
static let unknwonCase: Self = .unknown | |
case red = "RED" | |
case yelllow = "YELLOW" | |
case blue = "BLUE" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol UnknownCase: RawRepresentable, CaseIterable where RawValue: Equatable & Encodable { | |
static var unknownCase: Self { get } | |
} | |
extension UnknownCase { | |
init(rawValue: RawValue) { | |
let value = Self.allCases.first { $0.rawValue == rawValue } | |
self = value ?? Self.unknownCase | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct User: Codable { | |
let name: String | |
let preferences: [Preference] | |
} | |
struct Account: Codable { | |
let uuid: String | |
let email: String? | |
let address: Address? | |
} |