Applying Protocol-Oriented Programming in Development

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

Lesson 03: Protocols & SwiftUI

Demo 2

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

POP Demo

Head back to Xcode, where you’ll see how to use POP for the media shelf.

protocol MediaRepository {
    associatedtype Item: MediaItem & Identifiable & View
    
    func getItems() async throws -> [Item]
}
struct VideoGameMediaRepository: MediaRepository {
    func getItems() async throws -> [VideoGame] {
        [arkhamKnight, tearsOfTheKingdom].shuffled()
    }
}
let repository: any MediaRepository
@State var items: [T] = []
let view = MediaCollectionView<VideoGame>(repository: VideoGameMediaRepository())
.task {
    self.items = try! await repository.getItems() as! [T]
}
.refreshable {
    self.items = try! await repository.getItems() as! [T]
}
See forum comments
Cinema mode Download course materials from Github
Previous: Instruction 2 Next: Conclusion