Last active
November 23, 2019 22:42
-
-
Save GrantJEmerson/4affc05b02f128996b1cfab7fd49c9e2 to your computer and use it in GitHub Desktop.
Keying Into Tuples - SwiftMoji Entry #1
This file contains 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 HotDog { | |
var bun: String? | |
var meat: String? | |
var condiments: (chili: String?, cheese: String?, tomatoes: String?) | |
var ingredients: String { | |
[bun, meat, condiments.chili, condiments.cheese, condiments.tomatoes] | |
.compactMap { $0 } | |
.joined(separator: ", ") | |
} | |
mutating func remove(_ ingredients: Set<WritableKeyPath<Self, String?>>) { | |
ingredients.forEach { self[keyPath: $0] = nil } | |
} | |
} | |
struct Person { | |
let name: String | |
let dislikedIngredients: Set<WritableKeyPath<HotDog, String?>> | |
} | |
var π = HotDog(bun: "π₯", meat: "π¦", | |
condiments: (chili: "πΆ", cheese: "π§", tomatoes: "π ")) | |
let π¨βπ³ = Person( | |
name: "Timothy", | |
dislikedIngredients: [ | |
\HotDog.condiments.chili, | |
\HotDog.condiments.tomatoes | |
] | |
) | |
π.remove(π¨βπ³.dislikedIngredients) | |
print("π¨βπ³'s π has: \(π.ingredients)") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment