Question regarding class property default value

I’m reading Apple’s official Swift mannual (https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/AutomaticReferenceCounting.html#//apple_ref/doc/uid/TP40014097-CH20-ID48), and there’s something in the mannual that I don’t really understand.

 

class Country {

  let name: String

  var capitalCity: City!

  init(name: String, capitalName: String) {

  self.name = name

  self.capitalCity = City(name: capitalName, country: self)

  }

}

 

class City {

  let name: String

  unowned let country: Country

  init(name: String, country: Country) {

  self.name = name

  self.country = country

  }

 

Apple says: “Because capitalCity has a default nil value, a new Country instance is considered fully initialized as soon as the Country instance sets its name property within its initializer.”

 

But how does capitalCity have a default nil value? The code only says capitalCity takes values of type City!, but capitalCity has not been assigned any value yet. It says var capitalCity: City! not var capitalCity = City! .  So how can it have a default nil value already?

Powered by WPeMatico

About

You may also like...

Comments are closed.