Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- class MainViewController: UIViewController {
- func runScenario() {
- let user = User(name: "John")
- let iPhone = Phone(model: "iPhone Xs")
- user.add(phone: iPhone)
- }
- override func viewDidLoad() {
- super.viewDidLoad()
- runScenario()
- }
- class Phone {
- let model: String
- var owner: User?
- init(model: String) {
- self.model = model
- print("Phone \(model) was initialized")
- }
- deinit {
- print("Deallocating phone named: \(model)")
- }
- }
- class User {
- let name: String
- private(set) var phones: [Phone] = []
- func add(phone: Phone) {
- phones.append(phone)
- phone.owner = self
- }
- init(name: String) {
- self.name = name
- print("User \(name) was initialized")
- }
- deinit {
- print("Deallocating user named: \(name)")
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement