Skip to content

Instantly share code, notes, and snippets.

@PaulWoodIII
Last active August 4, 2019 00:18
Show Gist options
  • Select an option

  • Save PaulWoodIII/93461657ea8fd986273536c11af2a864 to your computer and use it in GitHub Desktop.

Select an option

Save PaulWoodIII/93461657ea8fd986273536c11af2a864 to your computer and use it in GitHub Desktop.
Wrapper around specialized datatype with SwiftUI Identifiable will work
//: [Previous](@previous)
import SwiftUI
import PlaygroundSupport
protocol Nameable {
var name: String { get }
}
struct Name: Nameable {
var name: String
public init(_ value: String) {
self.name = value
}
/// Imagine cool Name Stuff here that is unique to this type
/// But also notice how it is the same as Surname
}
struct Surname: Nameable {
var name: String
public init(_ value: String) {
self.name = value
}
/// Imagine cool Surname Stuff here that is unique to this type
/// But also notice how it is the same as Name
}
/// Wrapper around the specialized name types above
/// We know how this type will be used so now it conforms to Identifiable
/// instead of the models above
struct NameHolder: Nameable, Identifiable {
var id: UUID
var name: String
init<U>(nameable name: U) where U: Nameable {
self.name = name.name
self.id = UUID()
}
}
let names: [NameHolder] = [
NameHolder(nameable: Name("Alice")),
NameHolder(nameable: Name("Bob")),
NameHolder(nameable: Surname("Clark")),
NameHolder(nameable: Surname("Davis")),
NameHolder(nameable: Name("Elanore")),
NameHolder(nameable: Name("Frank")),
NameHolder(nameable: Surname("Gonzalez")),
NameHolder(nameable: Surname("Harris")),
NameHolder(nameable: Surname("Harris")), //Can Duplicate
NameHolder(nameable: Name("Harris")), // Can go across types
]
struct SimpleIdentifiableList : View {
var body: some View {
List(names) { name in
Text(name.name)
}
}
}
let viewController = UIHostingController(rootView: SimpleIdentifiableList())
PlaygroundPage.current.liveView = viewController
//: [Next](@next)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment