gpt4 book ai didi

swift - 有人可以帮助将 URLSession 从 Swift 转换为 Kotlin

转载 作者:行者123 更新时间:2023-12-04 03:26:09 25 4
gpt4 key购买 nike

<分区>

我想用 Kotlin 制作多平台天气应用程序。我还想在公共(public)模块中为 Android 和 iOS 单独获取天气数据。

对于 Android 我没有任何问题,但对于 iOS 部分我有。我想使用 Foundation 框架通过 URLSession 获取和序列化数据。但它不起作用。

这是我的天气模型。

    package com.example.weatherappversion3.shared

data class WeatherData (
val cod: String,
val message: Long,
val cnt: Long,
val list: List<ListElement>,
val city: City
)

data class City (
val id: Long,
val name: String,
val coord: Coord,
val country: String,
val population: Long,
val timezone: Long,
val sunrise: Long,
val sunset: Long
)

data class Coord (
val lat: Double,
val lon: Double
)

data class ListElement (
val dt: Long,
val main: MainClass,
val weather: List<Weather>,
val clouds: Clouds,
val wind: Wind,
val visibility: Long,
val pop: Double,
val sys: Sys,
val dtTxt: String,
val snow: Rain? = null,
val rain: Rain? = null
)

data class Clouds (
val all: Long
)

data class MainClass (
val temp: Double,
val feelsLike: Double,
val temp_min: Double,
val temp_max: Double,
val pressure: Long,
val seaLevel: Long,
val grndLevel: Long,
val humidity: Long,
val tempKf: Double
)

data class Rain (
val the3H: Double
)

data class Sys (
val pod: Pod
)

enum class Pod {
D,
N
}

data class Weather (
val id: Long,
val main: MainEnum,
val description: String,
val icon: String
)

enum class MainEnum {
Clear,
Clouds,
Rain,
Snow
}

data class Wind (
val speed: Double,
val deg: Long,
val gust: Double
)

这是我的要求:

    package com.example.weatherappversion3.shared

import com.example.weatherappversion3.shared.constants.Constants
//import cocoapods.Alamofire.*
import platform.Foundation.*
import platform.darwin.NSObject

//import SwiftyJSON

actual class WeatherManager {

actual var weatherData: WeatherData? = null
var data = NSData()
//https://api.openweathermap.org/data/2.5/forecast?appid=e83bce43f40758140ef6927fda5cfc85&units=metric&lat=48.51320000&lon=32.25970000
var url = "https://api.openweathermap.org/data/2.5/forecast?appid=${Constants().AppId}&units=${Constants().units}&lat=${Constants().lat}&lon=${Constants().lon}"
actual fun getWeatherForecastFor(updateUI: ()->Unit) {
println("Start request for iOS")
asyncRequest(url)
updateUI()
}

fun asyncRequest(url: String) {

val delegate = object : NSObject(), NSURLSessionDataDelegateProtocol {

override fun URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveData: NSData) {
data = didReceiveData
}

override fun URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError: NSError?) {
val response = task.response
if (response == null || (response as NSHTTPURLResponse).statusCode.toInt() != 200) {
return
}

if (didCompleteWithError != null) {
return
}

}
}

val session = NSURLSession.sessionWithConfiguration(
NSURLSessionConfiguration.defaultSessionConfiguration(),
delegate,
delegateQueue = NSOperationQueue.mainQueue()
)

session.dataTaskWithURL(NSURL(string = url)).resume()
}

actual fun getWeatherForecastForSpecialCity(city: String) {

}

}

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