0

When do you use this declaration in swift?

class Klass {
  var i:Int!
}

vs

class Klass {
  var i:Int
}

I know the latter has much strong checking, but i have been using the former occasionally when I do not have easy access to the init (like instantiating a viewcontroller from storyboard)

When are good scenarios for using the former?

3
  • 1
    Int! can be nil, but you the compiler will assume it's not. You have to be very careful when using it because if you try to access the value when it's nil, it will crash your app. They are called Implicitly Unwrapped Optionals. developer.apple.com/library/ios/documentation/Swift/Conceptual/… May 5, 2016 at 17:09
  • 1
    See stackoverflow.com/a/26112871/225650
    – Dominic K
    May 5, 2016 at 17:10
  • "A typical case of the latter usage is in a UIViewController, when a property is initialized in the viewDidLoad method rather than in an initializer - it makes sense to use an implicitly unwrapped. " -> thanks guys! makes a lot of sense now.
    – meow
    May 5, 2016 at 17:30

0

Browse other questions tagged or ask your own question.