Skip to content

Instantly share code, notes, and snippets.

View giovani-pereira-ifood's full-sized avatar

Giovani Nascimento Pereira giovani-pereira-ifood

View GitHub Profile
enum Icon {
case alert
case car
case person
...
}
extension Icon {
var stringValue: String? {
let items = JSONDecoder().decode([IconDescription], from: json)
[{
"name": "alert",
"char": "🔔"
},
{
"name": "car",
"char": "🚘"
}]
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 {
extension Icon {
func callAsFunction(color: UIColor = .black) -> IconView {
IconView(icon: self, color: color)
}
}
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
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}"
...
// {
// "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)
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"
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
}
struct User: Codable {
let name: String
let preferences: [Preference]
}
struct Account: Codable {
let uuid: String
let email: String?
let address: Address?
}