Skip to content

Instantly share code, notes, and snippets.

@GrantJEmerson
Last active November 23, 2019 22:42
Show Gist options
  • Save GrantJEmerson/4affc05b02f128996b1cfab7fd49c9e2 to your computer and use it in GitHub Desktop.
Save GrantJEmerson/4affc05b02f128996b1cfab7fd49c9e2 to your computer and use it in GitHub Desktop.
Keying Into Tuples - SwiftMoji Entry #1
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