Swift optional or Implicit property on UIViewController subclass

Leo Correa

I have a the following class

class Foo : UIViewController {
  var demo : SKView

  override func viewDidLoad() {
    ...
    self.demo = SKView(...)
    ...
    self.view.insertSubview(demo, atIndex: 0)
  }

  override func viewWillDisappear(animated : Bool) {
    self.demo.removeFromSuperView()
    self.demo = nil
  }
}

My question is, since I don't have an initializer on Foo, demo has to be either ? or !. I have read many places that I should stay away from using !. In this case however, is there any reason to not use it? I'm not 100% clear on the downsides specifically on this case since the view itself will always be set on viewDidLoad and only unset on viewWillDisappear.

What is the most common pattern for these kind of properties on such classes? Properties that get set on viewDidLoad and will always have a value.

Randy

The reason for this is that Swift enforces class members to always have a value upon initialization. By marking the variable demo with ! or ? you are telling the compiler that this variable can have the value nil, hence the optional notation.

In the context of iOS development, it's perfectly acceptable to mark SKView with ! so you don't have to suffix each call to demo with ?.

If demo is nil and you require a value to always be present then the app will crash during runtime which under many circumstances is in fact desirable.

class Foo : UIViewController {
  var demo : SKView!

  override func viewDidLoad() {
    ...
    self.demo = SKView(...)
    ...
    self.view.insertSubview(demo, atIndex: 0)
  }

  override func viewWillDisappear(animated : Bool) {
    self.demo.removeFromSuperView()
    self.demo = nil
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Non-optional var in swift UIViewController subclass

From Dev

Why is the 'view' property of UIViewController not optional in Swift?

From Dev

How to subclass custom UIViewController in Swift?

From Dev

Swift: UICollectionView into UIViewController programmatically as subclass

From Dev

Swift: Initialization of UIViewController subclass with custom parameters

From Dev

Swift: How to initialise a let constant in a UIViewController subclass

From Dev

Double Initialization Of UIViewController's Subclass Member in Swift

From Dev

Swift: How to initialise a let constant in a UIViewController subclass

From Dev

optional closure property in Swift

From Dev

Swift optional property error

From Dev

How to subclass UITableViewController with non-optional property

From Dev

How to create Non-optional stored properties for UIViewController Custom Subclass

From Dev

Swift Protocol - Property type subclass

From Dev

UIPickerView subclass in Swift : error optional value in viewForRow

From Dev

Swift: setting an optional property of a protocol

From Dev

Swift optional Array property is immutable?

From Dev

Swift optional Array property is immutable?

From Dev

Cocoa Binding to an optional property in swift

From Java

How do I make a custom initializer for a UIViewController subclass in Swift?

From Dev

Why you need to add empty constructors for UIViewController subclass in Swift

From Dev

Having a Swift class act as both a UIViewController subclass and a UITableViewDelegate

From Dev

Why does the Swift default initializer of a UIViewController subclass initialize the properties twice?

From Dev

Swift 2.0 Subclassing a subclass of UIViewController and called convenience initializers

From Dev

Xcode 7 Swift 2 impossible to instantiate UIViewController subclass of generic UITableViewController

From Dev

Why you need to add empty constructors for UIViewController subclass in Swift

From Dev

What is the right way to override a property in a subclass in Swift?

From Dev

Accessing a subclass property from a super class in Swift

From Dev

Accessing a subclass property from a super class in Swift

From Dev

Swift + CoreData: Cannot Automatically Set Optional Attribute On Generated NSManagedObject Subclass

Related Related

  1. 1

    Non-optional var in swift UIViewController subclass

  2. 2

    Why is the 'view' property of UIViewController not optional in Swift?

  3. 3

    How to subclass custom UIViewController in Swift?

  4. 4

    Swift: UICollectionView into UIViewController programmatically as subclass

  5. 5

    Swift: Initialization of UIViewController subclass with custom parameters

  6. 6

    Swift: How to initialise a let constant in a UIViewController subclass

  7. 7

    Double Initialization Of UIViewController's Subclass Member in Swift

  8. 8

    Swift: How to initialise a let constant in a UIViewController subclass

  9. 9

    optional closure property in Swift

  10. 10

    Swift optional property error

  11. 11

    How to subclass UITableViewController with non-optional property

  12. 12

    How to create Non-optional stored properties for UIViewController Custom Subclass

  13. 13

    Swift Protocol - Property type subclass

  14. 14

    UIPickerView subclass in Swift : error optional value in viewForRow

  15. 15

    Swift: setting an optional property of a protocol

  16. 16

    Swift optional Array property is immutable?

  17. 17

    Swift optional Array property is immutable?

  18. 18

    Cocoa Binding to an optional property in swift

  19. 19

    How do I make a custom initializer for a UIViewController subclass in Swift?

  20. 20

    Why you need to add empty constructors for UIViewController subclass in Swift

  21. 21

    Having a Swift class act as both a UIViewController subclass and a UITableViewDelegate

  22. 22

    Why does the Swift default initializer of a UIViewController subclass initialize the properties twice?

  23. 23

    Swift 2.0 Subclassing a subclass of UIViewController and called convenience initializers

  24. 24

    Xcode 7 Swift 2 impossible to instantiate UIViewController subclass of generic UITableViewController

  25. 25

    Why you need to add empty constructors for UIViewController subclass in Swift

  26. 26

    What is the right way to override a property in a subclass in Swift?

  27. 27

    Accessing a subclass property from a super class in Swift

  28. 28

    Accessing a subclass property from a super class in Swift

  29. 29

    Swift + CoreData: Cannot Automatically Set Optional Attribute On Generated NSManagedObject Subclass

HotTag

Archive