2
我以前使用RxSwift,我決定我不想再使用它,並能夠將所有內容轉換爲我更熟悉的Bond。由於新的變化,雖然債券V5,我似乎無法弄清楚如何觀察UserDefaults中的值。以下代碼最終給我一個致命的錯誤。ReactiveKit債券KVO觀察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
如果你設置ofType爲'Optional .self,'? –
是的,這是完美的工作。如果我把正確的類型放進去,那肯定會有所幫助。隨時回答,我會接受:) – SamG
很高興它的工作! :) –