1

I recently came across someone creating an instance of a class like this:

var myModel: MyModel!

I have not seen this before. Can someone please explain the difference between the above and the more familiar instantiation:

var myModel = MyModel()
Max
  • 33
  • 3
  • `MyModel!` does *not* create an instance. It's a type annotation that tells the compiler (dangerously) that `MyModel` will explicitly not be `nil` (although it may, in fact, be) – jnpdx Oct 03 '22 at 02:42
  • @jnpdx no it doesn't – matt Oct 03 '22 at 02:43
  • It is called 'Implicitely Unrwapped Optional Property'. It means that it can be nil but can be accessed without unwrapping its optional value. https://docs.swift.org/swift-book/LanguageGuide/AutomaticReferenceCounting.html#ID53 – rommex Oct 03 '22 at 05:49

0 Answers0