我正在阅读raywenderlich的Rx-Reactive Programming书籍,我对Rx Swift的这种语法感到困惑:
extension PHPhotoLibrary {
static var authorized: Observable<Bool> {
return Observable.create({ observer in
DispatchQueue.main.async {
if authorizationStatus() == .authorized {
observer.onNext(true)
observer.onCompleted()
} else {
observer.onNext(false)
requestAuthorization({ newStatus in
observer.onNext(newStatus == .authorized)
observer.onCompleted()
})
}
}
return Disposables.create()
})
}
}
为什么代码有两个return
语句?有人可以解释一下为什么我们在代码中需要2个return语句吗?
答案 0 :(得分:5)
如果您更好地格式化代码,您会看到第一个返回属于 static var authorized ,另一个属于observable。它与RxSwift无关,只是普通的Swift语法。
extension PHPhotoLibrary {
static var authorized: Observable<Bool> {
return Observable.create({ observer in
DispatchQueue.main.async {
if authorizationStatus() == .authorized {
observer.onNext(true)
observer.onCompleted()
} else {
observer.onNext(false)
requestAuthorization({ newStatus in
observer.onNext(newStatus == .authorized)
observer.onCompleted()
})
}
}
return Disposables.create()
})
}
}