Advertisement
wpl36

Untitled

Jun 27th, 2021
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1.  
  2.  
  3. import UIKit
  4.  
  5. class MainViewController: UIViewController {
  6.  
  7.  
  8. func runScenario() {
  9. let user = User(name: "John")
  10. let iPhone = Phone(model: "iPhone Xs")
  11. user.add(phone: iPhone)
  12. }
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21. override func viewDidLoad() {
  22. super.viewDidLoad()
  23. runScenario()
  24.  
  25. }
  26.  
  27.  
  28.  
  29.  
  30. class Phone {
  31. let model: String
  32. var owner: User?
  33. init(model: String) {
  34. self.model = model
  35. print("Phone \(model) was initialized")
  36. }
  37.  
  38. deinit {
  39. print("Deallocating phone named: \(model)")
  40. }
  41. }
  42.  
  43. class User {
  44. let name: String
  45. private(set) var phones: [Phone] = []
  46.  
  47. func add(phone: Phone) {
  48. phones.append(phone)
  49. phone.owner = self
  50. }
  51.  
  52. init(name: String) {
  53. self.name = name
  54. print("User \(name) was initialized")
  55. }
  56.  
  57. deinit {
  58. print("Deallocating user named: \(name)")
  59. }
  60. }
  61.  
  62.  
  63.  
  64.  
  65. }
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement