我有两组结构,感谢这些结构,我想通过API下载当前的天气和天气预报数据,但是有些结构具有相同的名称,然后Xcode通知我:
无效的“主要”声明
这意味着我具有相同名称的结构。 如何在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
}
答案 0 :(得分:1)
您可以如下编写天气预报模型。 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
}
答案 1 :(得分:0)
您需要更改名称,因为您在项目中的某个地方似乎具有相同的名称
struct MainJson : Decodable {
let temp, tempMin, tempMax : Double
let pressure, humidity : Int
}
然后将相关密钥替换为新名称,例如
let main : MainJson
这不会影响任何有效名称对json struct /类名称的解码,重要的是键名
let main << this : Main << not this