GregLeck

Structs Part II

Oct 1st, 2019
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.36 KB | None | 0 0
  1. // STRUCTS - PART II
  2. // Structs come with their own memberwise initializer
  3. // but we can provide our own init() method
  4. // to initialize all stored properties
  5. struct User {
  6.     var username: String
  7.    
  8.     init() {
  9.         username = "Anonymous"
  10.         print("We have a new user!")
  11.     }
  12. }
  13.  
  14. var person = User()
  15. person.username             // "Anonymous"
  16. person.username = "leck"
  17. print(person.username)      // "leck"
  18.  
  19. // SELF: REFERRING TO THE CURRENT INSTANCE
  20. // Useful when a property and init parameter share the same name
  21. struct Car {
  22.     var color: String
  23.    
  24.     init(color: String) {
  25.         self.color = color
  26.     }
  27. }
  28.  
  29. let buick = Car(color: "red")
  30. print(buick.color)              // "red"
  31.  
  32. // LAZY PROPERTIES
  33. // Property is only created when it is first accessed
  34. struct FamilyTree {
  35.     init() {
  36.         print("Creating family tree")
  37.     }
  38. }
  39.  
  40. struct Person {
  41.     var name: String
  42.     lazy var familyTree = FamilyTree()  // property not created unless accessed
  43.    
  44.     init(name: String) {
  45.         self.name = name
  46.     }
  47. }
  48.  
  49. var ed = Person(name: "ed")
  50. // Access familyTree property to see message:
  51. ed.familyTree       // "Creating family tree"
  52.  
  53. // ACCESS CONTROL
  54. // Use "private" to restict access to properties and functions
  55. // within a given struct
  56. // Struct's memberwise contructor not available with private properties
  57. struct PrivateData {
  58.     var name: String
  59.     private var idNumber: Int
  60.    
  61.     init(name: String, idNumber: Int) {
  62.         self.name = name
  63.         self.idNumber = idNumber
  64.     }
  65.    
  66.     func printTheId() {
  67.         print("The id is \(idNumber)")
  68.     }
  69.    
  70.     private func privateMessage() {
  71.         print("The id is \(idNumber)")
  72.     }
  73. }
  74.  
  75. let john = PrivateData(name: "John", idNumber: 9384092)
  76. john.name                        // "John"
  77. // john.idNumber                 // Not accessible
  78. john.printTheId()                // "The id is 9384092"
  79. // john.privateMessage()         // Not accessible
  80.  
  81. // STATIC PROPERTIES AND METHODS
  82. struct StaticExample {
  83.     var name: String
  84.     static var quote = "It's not who you are underneath, but what you do that defines you."
  85.    
  86.     init(name: String) {
  87.         self.name = name
  88.     }
  89. }
  90.  
  91. let bruceWayne = StaticExample(name: "Batman")
  92. bruceWayne.name                     // "Batman"
  93. StaticExample.quote                 // "It's not who..."
Advertisement
Add Comment
Please, Sign In to add comment