我有這個功能:
class myClass: ObservableObject {
func doSomethingElse<T:Decodable>(url: URL,
config: URLSessionConfiguration,
type: T.Type) -> AnyPublisher<Int, any Error> {
return URLSession(configuration:config).dataTaskPublisher(for: url)
.tryMap{ response in
guard let valueResponse = response.response as? HTTPURLResponse else {
return 000
}
return valueResponse.statusCode
}.mapError{error in
return error
}
.eraseToAnyPublisher()
}
}
它作業得很好,但我正在嘗試將此功能添加到URLSession擴展中:
extension URLSession {
func doSomethingElse<T:Decodable>(url: URL,
config: URLSessionConfiguration,
type: T.Type) -> AnyPublisher<Int, any Error> {
return self(configuration:config).dataTaskPublisher(for: url)
.tryMap{ response in
guard let valueResponse = response.response as? HTTPURLResponse else {
return 000
}
return valueResponse.statusCode
}.mapError{error in
return error
}
.eraseToAnyPublisher()
}
但我收到了這個錯誤:

Cannot call value of non-function type 'URLSession'
你們中的任何人都知道為什么我會收到此錯誤嗎?或者如何配置 URLSession 來進行配置?
我會非常感謝你的幫助
uj5u.com熱心網友回復:
self是 的具體實體URLSession。你想要Self的,就是型別
Self(configuration:config)...
您可能還想doSomethingElse成為一個static函式,因為它實際上并不參考 的特定實體self:
static func doSomethingElse<T:Decodable>(url: URL,
config: URLSessionConfiguration,
type: T.Type) -> AnyPublisher<Int, any Error> {
return Self(configuration:config).dataTaskPublisher(for: url)
.tryMap{ response in
guard let valueResponse = response.response as? HTTPURLResponse else {
return 000
}
return valueResponse.statusCode
}.mapError{error in
return error
}
.eraseToAnyPublisher()
}
uj5u.com熱心網友回復:
改變
func doSomethingElse...{
return self(configuration:...
至
static func doSomethingElse...{
return Self(configuration:...
稱它為
URLSession.doSomethingElse...
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/533042.html
標籤:迅速结合网址会话xcode14nsurlsession配置
上一篇:什么是blazor根組件?
