[英]iOS Swift4 - reInitialize lazy variable
Is there a way to reInitialize lazy variable based on the current language ? 有没有一种方法可以根据当前语言重新初始化惰性变量?
lazy var localizableDictionary: NSDictionary! = {
guard let path = Bundle.main.path(
forResource: "Localizable",
ofType: "strings",
inDirectory: nil,
forLocalization: Localizer.shared.currentLanguage)
else {
fatalError("Localizable file NOT found")
}
return NSDictionary(contentsOfFile: path)
}()
its a lazy var
clearly its a variable, so Swift will not stop you from modifying its value at any point in time if thats necessary. 它是一个lazy var
变量,显然是一个变量,因此,如果有必要,Swift不会在任何时间阻止您修改其值。
You can simply say at any point, 您可以随时说,
guard let path = Bundle.main.path(
forResource: "Localizable",
ofType: "strings",
inDirectory: nil,
forLocalization: Localizer.shared.currentLanguage)
else {
fatalError("Localizable file NOT found")
}
self.localizableDictionary = NSDictionary(contentsOfFile: path)
FYI FYI
Lazy initialization (also sometimes called lazy instantiation, or lazy loading) is a technique for delaying the creation of an object or some other expensive process until it's needed. 延迟初始化(有时也称为延迟实例化或延迟加载)是一种用于延迟对象或某些其他昂贵进程的创建直到需要它的技术。 When programming for iOS, this is helpful to make sure you utilize only the memory you need when you need it. 当为iOS编程时,这有助于确保仅在需要时使用所需的内存。
above quote copied from http://mikebuss.com/2014/06/22/lazy-initialization-swift/ 以上报价摘自http://mikebuss.com/2014/06/22/lazy-initialization-swift/
Please don't be under the impression that lazy var are constants if you really need a constant you will opt for let straight away :) 如果您确实需要一个常量,请不要以为惰性var是常量的印象就可以了:)
Hope it helps 希望能帮助到你
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.