我以前使用过RxSwift,我决定不再使用它了,并且能够把所有的东西都转换成邦德,这一点我更熟悉。由于邦德v5的新变化,我似乎不知道如何在UserDefaults中观察值。下面的代码最后给出了一个致命错误。
userDefaults.reactive
.keyPath(LocationManager.HomeLocationKey, ofType: String.self, context: .immediateOnMain)
.map(self.initLocation(from:))
.bind(to: self.homeLocation)userDefaults是对UserDefaults.standard的引用,LocationManager.HomeLocationKey是字符串。我正在提供下面的initLocation函数,因为我知道它将被要求。在该功能下面,我将发布在应用程序启动后收到的错误。
func initLocation(from string: String?) -> Location?
{
guard let dataString = string
else { log.warning("Location data did not exist, returning nil"); return nil }
let json = JSON.parse(dataString)
return Location(from: json)
}错误:
fatal error: Could not convert nil to String. Maybe `dynamic(keyPath:ofExpectedType:)` method might be of help?): file /Users/sam/Documents/iOS Apps/Drizzle/Pods/Bond/Sources/Shared/NSObject+KVO.swift, line 58发布于 2017-06-14 10:45:29
这可能并不明显,但如果观察到的值可以为零,则ofType参数必须是可选类型。就你而言,这将是:
userDefaults.reactive
.keyPath(LocationManager.HomeLocationKey, ofType: Optional<String>.self, context: .immediateOnMain)
...https://stackoverflow.com/questions/44531293
复制相似问题