Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

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

Select an option

Save PaulWoodIII/a81dc7290e5b791c3c8571ec5b8b7c48 to your computer and use it in GitHub Desktop.
Protocol Associated Types with SwiftUI Identifiable will not work
//: [Previous](@previous)
import Foundation
import SwiftUI
import UIKit
import PlaygroundSupport
// This doesnt work because the protocol has an assoicated Type
// To understand more
// https://chariotsolutions.com/screencast/philly-ete-2019-rob-napier-generic-swift-it-isnt-supposed-to-hurt/
//
protocol Nameable: Identifiable {
var name: String { get }
}
struct Name: Nameable {
var name: String
public init(_ value: String) {
self.name = value
}
var id: String { return name }
}
struct Surname: Nameable {
var name: String
public init(_ value: String) {
self.name = value
}
var id: String { return name }
}
let names: [Nameable] = [
Name("Alice"),
Name("Bob"),
Surname("Clark"),
Surname("Davis"),
Name("Elanore"),
Name("Frank"),
Surname("Gonzalez"),
Surname("Harris"),
]
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