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?

meow
  • 27,476
  • 33
  • 116
  • 177
  • 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`. https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html – EmilioPelaez May 05 '16 at 17:09
  • 1
    See http://stackoverflow.com/a/26112871/225650 – Dominic K May 05 '16 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 05 '16 at 17:30

0 Answers0