gpt4 book ai didi

ios - ReactiveSwift 中的 API 请求

转载 作者:行者123 更新时间:2023-11-28 23:48:12 25 4
gpt4 key购买 nike

我是 ReactiveSwift 的初学者。我创建了天气应用程序,但我的请求不起作用。

func fetchCurrentWeather() -> SignalProducer<TodayWeatherData?, DownloadError> {
guard let unwrappedURL = url else { return SignalProducer.empty }

return URLSession.shared.reactive
.data(with: URLRequest(url: unwrappedURL))
.retry(upTo: 2)
.flatMapError { error in
print("Error = \(error.localizedDescription)")
return SignalProducer.empty
}
.map { (data, response) -> TodayWeatherData? in
do {
let weatherArray = try JSONDecoder().decode(TodayWeatherData.self, from: data)
return weatherArray
} catch (let error) {
print("\(error)")
return nil
}
}
.observe(on: UIScheduler())
}

self.weatherFetcher.fetchCurrentWeather().map { weather in

}

不调用 map block 。我应该在此请求或解析方法中更改什么?

最佳答案

您必须启动您的SignalProducer

self.weatherFetcher.fetchCurrentWeather().startWithResult({ result in 
switch result {
case .success(let weather): //use the result value
case .failed(let error): //handle the error
}

})

你还有

  • 开始失败()
  • startWithValues()
  • startWithCompleted()
  • 开始()

在所有情况下,您都必须“启动”冷信号才能使它们起作用。

关于ios - ReactiveSwift 中的 API 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52258986/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com