Understanding Optional Chaining in Swift Programming

Slide Note
Embed
Share

Learn about optional chaining in Swift programming with examples involving students, dormitories, and room numbers. Explore how to handle optionals to avoid crashes and safely access nested properties.


Uploaded on Oct 01, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Ch14.

  2. 14.1 (?) nil

  3. // optional chaining class Student { var dorm: Dormitory? } class Dormitory { var numberOfRooms = 2 } let peter = Student() let rooms = peter.dorm?.numberOfRooms print("Dormitory has \(rooms) rooms")

  4. Dormitory has nil rooms

  5. 14.2 class Student { var dorm: Dormitory? } init(numberOfRooms: Int) { self.numberOfRooms = numberOfRooms } var location: Location? } class Dormitory { var numberOfRooms: Int class Location { var dormitoryName: String? var street: String? } func printNumberOfRooms() { print("The number of rooms is \(numberOfRooms)") }

  6. 14.2.1

  7. class Student { var dorm: Dormitory? } class Location { var dormitoryName: String? var street: String? } class Dormitory { var numberOfRooms: Int let peter = Student() if let roomNumber = peter.dorm?.numberOfRooms { print("Peter's dormitory has \(roomNumber) rooms") } else { print("Unabel to retrieve the number of rooms") } func printNumberOfRooms() { print("The number of rooms is \(numberOfRooms)") } init(numberOfRooms: Int) { self.numberOfRooms = numberOfRooms } var location: Location? }

  8. Unabel to retrieve the number of rooms

  9. 14.2.2 printNumberOfRooms

  10. class Student { var dorm: Dormitory? } class Location { var dormitoryName: String? var street: String? } class Dormitory { var numberOfRooms: Int // call method let peter = Student() peter.dorm = Dormitory(numberOfRooms: 10) peter.dorm!.printNumberOfRooms() func printNumberOfRooms() { print("The number of rooms is \(numberOfRooms)") } init(numberOfRooms: Int) { self.numberOfRooms = numberOfRooms } var location: Location? }

  11. The number of rooms is 10

  12. 14.3 Location peterLocation Location peterLocation peter.dorm?.location

  13. class Student { var dorm: Dormitory? } class Location { var dormitoryName: String? var street: String? } class Dormitory { var numberOfRooms: Int func printNumberOfRooms() { print("The number of rooms is \(numberOfRooms)") } init(numberOfRooms: Int) { self.numberOfRooms = numberOfRooms } var location: Location? } // multiple chain let peter = Student() peter.dorm = Dormitory(numberOfRooms: 10) let peterLocation = Location() peterLocation.dormitoryName = "BigHouse Building" peterLocation.street = "Hsingchang 777" peter.dorm?.location = peterLocation print(peter.dorm?.location?.dormitoryName) print(peter.dorm?.location?.street)

  14. Optional(BigHouse Building) Optional(Hsingchang 777)

Related


More Related Content