GregLeck

Structs - Part O

Sep 30th, 2019
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.71 KB | None | 0 0
  1. // STRUCTS (Part I)
  2.  
  3. // Struct with one "stored" property
  4. struct Sport {
  5.     var name: String
  6. }
  7.  
  8. var tennis = Sport(name: "Tennis")
  9. print(tennis.name)
  10.  
  11. // tennis and name are variables, so they can be changed
  12. tennis.name = "Lawn Tennis"
  13.  
  14. // COMPUTED PROPERTIES
  15. // New struct with two stored properties
  16. // and one computed property:
  17. // NB: Constants cannot be computed properties
  18. struct Sport2 {
  19.     var name: String
  20.     var isOlympicSport: Bool
  21.    
  22.     var olympicStatus: String  {
  23.         if isOlympicSport {
  24.             return "\(name) is an Olympic sport"
  25.         } else {
  26.             return "\(name) is not an Olympic sport"
  27.         }
  28.     }
  29. }
  30. let taiChi = Sport2(name: "Tai Chi", isOlympicSport: false)
  31. taiChi.olympicStatus    // "Tai Chi is not an Olympic sport"
  32.  
  33. // PROPERTY OBSERVERS
  34. // willSet{}: Used before a property is changed
  35. // didSet{}: Used after a property is changed
  36. struct Progress {
  37.     var amount: Int {
  38.         willSet {
  39.             print("Progress is about to change.")
  40.         }
  41.         didSet {
  42.             print("Progress is now \(amount) percent")
  43.         }
  44.     }
  45. }
  46.  
  47. var progress = Progress(amount: 20)
  48. print(progress.amount)      // "20"
  49. progress.amount = 40        // "Progress is about to change."
  50.                             // "Progress is now 40 percent"
  51.  
  52. // METHODS
  53. // Functions inside Structs are called methods
  54. struct City {
  55.     var population: Int
  56.    
  57.     func collectTaxes() -> Int {
  58.         return population * 1000
  59.     }
  60. }
  61.  
  62. let smallCity = City(population: 10_000)
  63. print("We can collect \(smallCity.collectTaxes()) in taxes!")
  64.  
  65. // MUTATING METHODS
  66. // Swift won't allow a struct's method to change it's associated properties,
  67. // unless the "mutating func" keywords are used
  68. struct Person {
  69.     var name: String
  70.    
  71.     mutating func changeNameToFred() {
  72.         name = "Fred"
  73.     }
  74. }
  75.  
  76. var jim = Person(name: "Jim")
  77. jim.changeNameToFred()
  78. jim.name                // "Fred"
  79.  
  80. // PROPERTIES AND METHODS OF STRINGS
  81. // Strings are structs!
  82. // And have their own properties and methods
  83. let string = "Do or do not, there is no try."
  84.  
  85. // count property
  86. string.count
  87.  
  88. // various methods:
  89. string.hasPrefix("Do")          // return true
  90. string.uppercased()             // everything now uppercased
  91. string.sorted()                 // returns array
  92.  
  93. // PROPERTIES AND METHODS OF ARRAYS
  94. // Arrays aare also structs!
  95. // Yep, they've got their own properties and methods
  96. var beatles = ["John", "Paul", "George", "Ringo"]
  97.  
  98. // count property
  99. beatles.count
  100.  
  101. // add new item method
  102. beatles.append("Fifth Beatle")
  103.  
  104. // locate item method
  105. beatles.firstIndex(of: "John")
  106.  
  107. // sort method
  108. beatles.sorted()
  109.  
  110. // remove item method
  111. beatles.remove(at: 0)
Advertisement
Add Comment
Please, Sign In to add comment