Introduction to Object-Oriented Programming

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

Lesson 04: Protocols & Interfaces

Demo

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 make MuseumObject conform to Equatable and CustomStringConvertible. Then you’ll create a protocol and a subclass of MuseumObject that adopts it.

extension MuseumObject: Equatable {
}
Type 'MuseumObject' does not conform to protocol 'Equatable'
Do you want to add protocol stubs?
static func == (lhs: MuseumObject, rhs: MuseumObject) -> Bool {
  <#code#>
}
lhs.objectID == rhs.objectID
let object =
MuseumObject(objectID: 436535,
             title: "Wheat Field with Cypresses",
             objectURL: "https://www.metmuseum.org/art/collection/search/436535",
             creditLine: "Purchase, The Annenberg Foundation Gift, 1993",
             isPublicDomain: true)
             
let object_pd =
PublicDomainObject(objectID: 436535,
                   title: "Wheat Field with Cypresses",
                   objectURL: "https://www.metmuseum.org/art/collection/search/436535",
                   primaryImageSmall: "https://images.metmuseum.org/CRDImages/ep/original/DT1567.jpg"
                   creditLine: "Purchase, The Annenberg Foundation Gift, 1993")
object == object_pd
let object2 =
MuseumObject(objectID: 13061,
             title: "Cypress and Poppies",
             objectURL: "https://www.metmuseum.org/art/collection/search/13061",
             creditLine: "Gift of Iola Stetson Haverstick, 1982",
             isPublicDomain: false)
object2 == object
print(object)
extension MuseumObject: CustomStringConvertible {
  var description: String {
    "\(title): \(creditLine)"
  }
}
Wheat Field with Cypresses: Purchase, The Annenberg Foundation Gift, 1993
object2.showImage()
protocol OnDisplay {
  var GalleryNumber: String { get }
  func showMap(from: String, to: String)
}
class OnDisplayObject: MuseumObject {
  let GalleryNumber: String

  init(objectID: Int,
       title: String,
       objectURL: String,
       creditLine: String,
       GalleryNumber: String,
       isPublicDomain: Bool) {
    self.GalleryNumber = GalleryNumber
    super.init(objectID: objectID,
               title: title,
               objectURL: objectURL,
               creditLine: creditLine,
               isPublicDomain: isPublicDomain)
  }
} 
extension OnDisplayObject: OnDisplay {
  func showMap(from: String, to: String) {
    guard !GalleryNumber.isEmpty else { return }
    // implementation
  }
}
let object_od =
OnDisplayObject(objectID: 436535,
                title: "Wheat Field with Cypresses",
                objectURL: "https://www.metmuseum.org/art/collection/search/436535",
                creditLine: "Purchase, The Annenberg Foundation Gift, 1993",
                GalleryNumber: "199",
                isPublicDomain: true)
object_od.showImage()
See forum comments
Cinema mode Download course materials from Github
Previous: Instruction Next: Conclusion