Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.01 KB | None | 0 0
  1. import Foundation
  2.  
  3.  
  4. struct Student {
  5.     var firstName: String {
  6.         willSet {
  7.             print("We will set to \(firstName) new name \(newValue)")
  8.         }
  9.         didSet {
  10.             print("New name \(firstName) has been setted instead of \(oldValue)")
  11.             firstName = firstName.capitalized
  12.         }
  13.     }
  14.     var surname: String {
  15.         didSet {
  16.             print("New surname \(surname) has been setted instead of \(oldValue)")
  17.             surname = surname.capitalized
  18.         }
  19.     }
  20.    
  21.     var fullName: String {
  22.         get {
  23.             return firstName + " " + surname
  24.         }
  25.         set {
  26.             let words = newValue.components(separatedBy: " ")
  27.             if words.count > 0 {
  28.                 firstName = words[0]
  29.             }
  30.             if words.count > 1 {
  31.                 surname = words[1]
  32.             }
  33.         }
  34.     }
  35.    
  36.    
  37.    
  38.    
  39.    
  40. }
  41.  
  42. var st = Student(firstName: "John", surname: "Webb")
  43.  
  44. st.fullName = "maX kaRAtai "
  45. print(st)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement