这是我的代码段:
open class SomeClass {
let driver: Driver<Bool>
init(input: Observable<String>) {
driver = input
.map( { s -> Bool in self.convert(text: s) }) // error 'self' captured by a closure before all members were initialized
.asDriver(onErrorJustReturn: false)
}
func convert(text: String) -> Bool {
// Do some complex calculation
return true
}
}
说明:在我的SomeClass中,我有一个类型为driver
的Driver<Bool>
对象,然后在我的init
中,我将获得一个Observable<String>
并将其映射到Observable<Bool>
。但是,为了进行转换,我需要在映射闭包内调用func convert()
,因此出现错误
'self'在所有成员初始化之前被闭包捕获
有人可以告诉我如何解决这个问题吗?
最简单的解决方案是将convert
移出该类:
open class SomeClass {
let driver: Driver<Bool>
init(input: Observable<String>) {
driver = input
.map( { s -> Bool in convert(text: s) }) // error 'self' captured by a closure before all members were initialized
.asDriver(onErrorJustReturn: false)
}
}
func convert(text: String) -> Bool {
// Do some complex calculation
return true
}