无法将类型 'Promise<T>' 的值分配给类型 'Promise<_>?'
Cannot assign value of type 'Promise<T>' to type 'Promise<_>?'
我正在尝试 API 请求,但是我收到错误消息
Cannot assign value of type 'Promise<T>' to type 'Promise<_>?'
我不知道 'Promise<_>?' 在哪里出现。
看一下代码:在 'as! Promise'
处抛出错误
class Cache<T> {
private let defaultMaxCacheAge: TimeInterval
private let defaultMinCacheAge: TimeInterval
private let endpoint: () -> Promise<T>
private let cached = CachedValue<T>()
private var pending: Promise<T>?
// Always makes API request, without regard to cache
func fresh() -> Promise<T> {
// Limit identical API requests to one at a time
if pending == nil {
pending = endpoint().then { freshValue in
self.cached.value = freshValue
return .value(freshValue)
}.ensure {
self.pending = nil
} as! Promise<T>
}
return pending!
}
//...More code...
}
和Cache的init
init(defaultMaxCacheAge: TimeInterval = .OneHour, defaultMinCacheAge: TimeInterval = .OneMinute, endpoint: @escaping () -> Promise<T>) {
self.endpoint = endpoint
self.defaultMaxCacheAge = defaultMaxCacheAge
self.defaultMinCacheAge = defaultMinCacheAge
}
和缓存值
class CachedValue<T> {
var date = NSDate.distantPast
var value: T? { didSet { date = NSDate() as Date } }
}
您似乎对在 PromiseKit (migration guide to PMK6) 中使用 then
/map
感到困惑:
then
is fed the previous promise value and requires you return a promise.
map
is fed the previous promise value and requires you return a non-promise, ie. a value.
因此,您应该将 then
更改为 map
到 return 和 T
,而不是 Promise<T>
in closure:
pending = endpoint()
.map { freshValue in
self.cached.value = freshValue
return freshValue
}.ensure {
self.pending = nil
}
我正在尝试 API 请求,但是我收到错误消息
Cannot assign value of type 'Promise<T>' to type 'Promise<_>?'
我不知道 'Promise<_>?' 在哪里出现。
看一下代码:在 'as! Promise'
处抛出错误class Cache<T> {
private let defaultMaxCacheAge: TimeInterval
private let defaultMinCacheAge: TimeInterval
private let endpoint: () -> Promise<T>
private let cached = CachedValue<T>()
private var pending: Promise<T>?
// Always makes API request, without regard to cache
func fresh() -> Promise<T> {
// Limit identical API requests to one at a time
if pending == nil {
pending = endpoint().then { freshValue in
self.cached.value = freshValue
return .value(freshValue)
}.ensure {
self.pending = nil
} as! Promise<T>
}
return pending!
}
//...More code...
}
和Cache的init
init(defaultMaxCacheAge: TimeInterval = .OneHour, defaultMinCacheAge: TimeInterval = .OneMinute, endpoint: @escaping () -> Promise<T>) {
self.endpoint = endpoint
self.defaultMaxCacheAge = defaultMaxCacheAge
self.defaultMinCacheAge = defaultMinCacheAge
}
和缓存值
class CachedValue<T> {
var date = NSDate.distantPast
var value: T? { didSet { date = NSDate() as Date } }
}
您似乎对在 PromiseKit (migration guide to PMK6) 中使用 then
/map
感到困惑:
then
is fed the previous promise value and requires you return a promise.
map
is fed the previous promise value and requires you return a non-promise, ie. a value.
因此,您应该将 then
更改为 map
到 return 和 T
,而不是 Promise<T>
in closure:
pending = endpoint()
.map { freshValue in
self.cached.value = freshValue
return freshValue
}.ensure {
self.pending = nil
}