使用依赖于其他实例变量的值初始化惰性实例变量
Initialize lazy instance variable with value that depends on other instance variables
以下初始化当前在调用 getEventCalendar
的行中产生此错误:
Cannot use instance member 'getEventCalendar' within property
initializer; property initializers run before 'self' is available.
是否有任何合适的方法来初始化 lazy
实例变量,其值取决于 self
的其他对象类型 instance variables
(不仅仅是 self
) ?我有例如尝试将 getEventCalendar
从方法转换为函数,但这也无济于事。
class AbstractEventCalendarClient {
let eventStore: EKEventStore
let entityType: EKEntityType
lazy var eventCalendar = getEventCalendar()
init(eventStore: EKEventStore, entityType: EKEntityType) {
self.eventStore = eventStore
self.entityType = entityType
}
func getEventCalendar() -> EKCalendar? {
// ...
}
}
您可以使用只执行一次的闭包,它捕获 self
的属性并在执行时使用这些属性(= 首次使用 lazy
属性)。例如
class Foo {
var foo: Int
var bar: Int
lazy var lazyFoobarSum: Int = { return self.foo + self.bar }()
init(foo: Int, bar: Int) {
self.foo = foo
self.bar = bar
}
}
let foo = Foo(foo: 2, bar: 3)
foo.foo = 7
print(foo.lazyFoobarSum) // 10
W.r.t。你自己的尝试:你可以用同样的方式,在这个只执行一次的闭包中使用self
的帮助(实例)函数。
class Foo {
var foo: Int
var bar: Int
lazy var lazyFoobarSum: Int = { return self.getFooBarSum() }()
init(foo: Int, bar: Int) {
self.foo = foo
self.bar = bar
}
func getFooBarSum() -> Int { return foo + bar }
}
let foo = Foo(foo: 2, bar: 3)
foo.foo = 7
print(foo.lazyFoobarSum) // 10
这是一条令人困惑的错误消息(您可能希望 file a bug report on). The problem is just a quirk of lazy
properties – they currently require an explicit use of self
in order to access instance members, as well as an explicit type annotation when doing so (which has been previously noted in )。
所以你需要说:
lazy var eventCalendar: EKCalendar? = self.getEventCalendar()
以下初始化当前在调用 getEventCalendar
的行中产生此错误:
Cannot use instance member 'getEventCalendar' within property initializer; property initializers run before 'self' is available.
是否有任何合适的方法来初始化 lazy
实例变量,其值取决于 self
的其他对象类型 instance variables
(不仅仅是 self
getEventCalendar
从方法转换为函数,但这也无济于事。
class AbstractEventCalendarClient {
let eventStore: EKEventStore
let entityType: EKEntityType
lazy var eventCalendar = getEventCalendar()
init(eventStore: EKEventStore, entityType: EKEntityType) {
self.eventStore = eventStore
self.entityType = entityType
}
func getEventCalendar() -> EKCalendar? {
// ...
}
}
您可以使用只执行一次的闭包,它捕获 self
的属性并在执行时使用这些属性(= 首次使用 lazy
属性)。例如
class Foo {
var foo: Int
var bar: Int
lazy var lazyFoobarSum: Int = { return self.foo + self.bar }()
init(foo: Int, bar: Int) {
self.foo = foo
self.bar = bar
}
}
let foo = Foo(foo: 2, bar: 3)
foo.foo = 7
print(foo.lazyFoobarSum) // 10
W.r.t。你自己的尝试:你可以用同样的方式,在这个只执行一次的闭包中使用self
的帮助(实例)函数。
class Foo {
var foo: Int
var bar: Int
lazy var lazyFoobarSum: Int = { return self.getFooBarSum() }()
init(foo: Int, bar: Int) {
self.foo = foo
self.bar = bar
}
func getFooBarSum() -> Int { return foo + bar }
}
let foo = Foo(foo: 2, bar: 3)
foo.foo = 7
print(foo.lazyFoobarSum) // 10
这是一条令人困惑的错误消息(您可能希望 file a bug report on). The problem is just a quirk of lazy
properties – they currently require an explicit use of self
in order to access instance members, as well as an explicit type annotation when doing so (which has been previously noted in
所以你需要说:
lazy var eventCalendar: EKCalendar? = self.getEventCalendar()