Applying Protocol-Oriented Programming in Development

Oct 17 2023 · Swift 5.9, iOS 17, Xcode 15

Lesson 03: Protocols & SwiftUI

Demo 1

Episode complete

Play next episode

Next

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Demo

In this demo, you’ll take your knowledge from the previous lessons and build some views using protocols. Open the starter playground in Xcode and run it. A simple view will appear.

struct MediaCollectionView<T: MediaItem>: MediaCollection & View {
    var items: [T]
}
var body: some View {

}
Text("Hello SwiftUI")
let view = MediaCollectionView(items: [arkhamKnight, tearsOfTheKingdom])
PlaygroundPage.current.setLiveView(view.frame(width: 375, height: 667))
struct MediaCollectionView<T: MediaItem & Identifiable>: MediaCollection & View
var body: some View {
    List(items) {
        Text($0.title)
    }
}
extension MediaItem {
    var body: some View {
        Text(self.title)
    }
}
struct MediaCollectionView<T: MediaItem & Identifiable & View>: MediaCollection & View 
var body: some View {
    List(items) {
        $0
    }
}
extension VideoGame: View {}
See forum comments
Cinema mode Download course materials from Github
Previous: Instruction Next: Instruction 2