gpt4 book ai didi

ios - 'Struct' 和 API 的重新声明无效

转载 作者:行者123 更新时间:2023-11-28 13:40:25 24 4
gpt4 key购买 nike

我有两组结构,感谢它们我想通过 API 下载当前天气和天气预报数据,但有些结构具有相同的名称,然后 Xcode 通知我:

Invalid redeclaration of 'Main'

这意味着我有同名的结构。如何在 Swift 中更改结构的名称以消除此错误并且不影响数据下载?

电流:

import Foundation

struct CurrentWeather : Decodable {

let coord : Coordinate
let cod, visibility, id : Int
let name : String
let base : String
let weather : [Weather]
let clouds: Clouds
let sys : Sys
let main : Main
let wind : Wind
let dt : Date
}

struct Coordinate : Decodable {
let lat, lon : Double

}

struct Weather : Decodable {
let id : Int
let icon : String
let main : MainEnum
let description: String
}

struct Sys : Decodable {
let type, id : Int
let sunrise, sunset : Date
let message : Double
let country : String
}

struct Main : Decodable {
let temp, tempMin, tempMax : Double
let pressure, humidity : Int
}

struct Wind : Decodable {
let speed : Double
let deg : Int
let gust : Double?
}

struct Clouds: Decodable {
let all : Int
}

enum MainEnum: String, Decodable {
case clear = "Clear"
case cloud = "Clouds"
case rain = "Rain"
case base = "Base"
case description = "Description"

case failure = "Failure"
case success = "Success"
}

预测:

 struct WeatherForecast : Decodable {

let cod : String
let message : Float
let cnt : Int
let list : [List]
let city : City
}

struct City : Decodable {

let id : Int
let name : String
let coord : Coord
let country : String
}

struct Coord : Decodable {

let lat : Float
let lon : Float
}

struct List : Decodable {

let dt : Int
let main : Main
let weather : [Weather]
let clouds : Cloud
let wind : Wind
let snow : Snow
let sys : Sy
let dtTxt : String
}

struct Sy : Decodable {

let pod : String
}

struct Snow : Decodable {

}

struct Wind : Decodable {

let speed : Float
let deg : Float
}

struct Clouds : Decodable {

let all : Int
}

struct Weather : Decodable {

let id : Int
let main : String
let descriptionField : String
let icon : String
}

struct Main : Decodable {

let temp : Float
let tempMin : Float
let tempMax : Float
let pressure : Int
let seaLevel : Float
let grndLevel : Int
let humidity : Int
let tempKf : Int
}

最佳答案

你可以写出如下的天气预报模型。 CurrentWeather 模型没有变化

////////////////////
struct WeatherForecast : Decodable {

let cod : String
let message : Float
let cnt : Int
let list : [List]
let city : City
}

struct City : Decodable {

let id : Int
let name : String
let coord : Coord
let country : String
}

struct Coord : Decodable {

let lat : Float
let lon : Float
}

struct List : Decodable {

let dt : Int
let main : MainWF
let weather : [WeatherWF]
let clouds : CloudsWF
let wind : WindWF
let snow : Snow
let sys : Sy
let dtTxt : String
}

struct Sy : Decodable {
let pod : String
}

struct Snow : Decodable {

}

struct WindWF : Decodable {

let speed : Float
let deg : Float
}

struct CloudsWF : Decodable {

let all : Int
}

struct WeatherWF : Decodable {

let id : Int
let main : String
let descriptionField : String
let icon : String
}

struct MainWF : Decodable {

let temp : Float
let tempMin : Float
let tempMax : Float
let pressure : Int
let seaLevel : Float
let grndLevel : Int
let humidity : Int
let tempKf : Int
}

关于ios - 'Struct' 和 API 的重新声明无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56112720/

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