隐式展开 Optional 当在初始化期间无法定义常量时,但会出现错误
Implicitly Unwrapped Optional when a constant that cannot be defined during initialisation, But Error Comes
Drewag 对 the question
的回答
Every member constant must have a value by the time initialization is complete. Sometimes, a constant cannot be initialized with its correct value during initialization, but it can still be guaranteed to have a value before being accessed.
Using an Optional variable gets around this issue because an Optional is automatically initialized with nil and the value it will eventually contain will still be immutable. However, it can be a pain to be constantly unwrapping a variable that you know for sure is not nil. Implicitly Unwrapped Optionals achieve the same benefits as an Optional with the added benefit that one does not have to explicitly unwrap it everywhere.
下面的代码定义了两个classes,Country
和City
,每个都将另一个class的实例存储为属性 .每个国家都必须有一个首都,每个城市都必须永远属于一个国家。
class Country {
let name: String
var capitalCity: City! //why I can't use let!
init(name: String, capitalName: String){
self.name = name
self.capitalCity = City(name: capitalName, country: self)
}
deinit {
print("\(name) has been de-initialized the city \(capitalCity.name) is gone with with the country")
}
}
class City{
let name: String
unowned let country: Country
init(name: String, country: Country){
self.name = name
self.country = country
}
deinit {
print("The city \(name) has been de-initialized ")
}
}
var country = Country(name: "Canada", capitalName: "Ottawa")
}
但是,如果我将 var capitalCity: City!
行更改为 let capitalCity: City!
,编译器会发出以下错误警告。
问题: 是不是我们可以在初始化时无法定义常量的情况下使用隐式解包可选?这里有什么错误?如果我理解有误,希望您能指出并帮助我。
重要的部分在这里:
City(name: capitalName, country: self)
您肯定在 属性 capitalCity
.
赋值之前执行的表达式中使用了 self
如果你想在任何表达式中使用self
,它需要在two phase initialization的第二阶段,这意味着在使用[=之前需要初始化所有属性12=].
使用 var
,Swift 为 属性 capitalCity
分配默认初始值 nil
。所以属性可以认为是"already initialized",所以,你可以在初始化另一个属性后使用self
name
。
(你知道将 nil
赋给 ImplicitlyUnwrappedOptional
的常量是荒谬的。)
顺便说一下,private(set) var
经常用于类似的情况:
private(set) var capitalCity: City!
Drewag 对 the question
的回答Every member constant must have a value by the time initialization is complete. Sometimes, a constant cannot be initialized with its correct value during initialization, but it can still be guaranteed to have a value before being accessed.
Using an Optional variable gets around this issue because an Optional is automatically initialized with nil and the value it will eventually contain will still be immutable. However, it can be a pain to be constantly unwrapping a variable that you know for sure is not nil. Implicitly Unwrapped Optionals achieve the same benefits as an Optional with the added benefit that one does not have to explicitly unwrap it everywhere.
下面的代码定义了两个classes,Country
和City
,每个都将另一个class的实例存储为属性 .每个国家都必须有一个首都,每个城市都必须永远属于一个国家。
class Country {
let name: String
var capitalCity: City! //why I can't use let!
init(name: String, capitalName: String){
self.name = name
self.capitalCity = City(name: capitalName, country: self)
}
deinit {
print("\(name) has been de-initialized the city \(capitalCity.name) is gone with with the country")
}
}
class City{
let name: String
unowned let country: Country
init(name: String, country: Country){
self.name = name
self.country = country
}
deinit {
print("The city \(name) has been de-initialized ")
}
}
var country = Country(name: "Canada", capitalName: "Ottawa")
}
但是,如果我将 var capitalCity: City!
行更改为 let capitalCity: City!
,编译器会发出以下错误警告。
问题: 是不是我们可以在初始化时无法定义常量的情况下使用隐式解包可选?这里有什么错误?如果我理解有误,希望您能指出并帮助我。
重要的部分在这里:
City(name: capitalName, country: self)
您肯定在 属性 capitalCity
.
self
如果你想在任何表达式中使用self
,它需要在two phase initialization的第二阶段,这意味着在使用[=之前需要初始化所有属性12=].
使用 var
,Swift 为 属性 capitalCity
分配默认初始值 nil
。所以属性可以认为是"already initialized",所以,你可以在初始化另一个属性后使用self
name
。
(你知道将 nil
赋给 ImplicitlyUnwrappedOptional
的常量是荒谬的。)
顺便说一下,private(set) var
经常用于类似的情况:
private(set) var capitalCity: City!