Ios 对'的重新声明无效;结构';及空气污染指数

Ios 对'的重新声明无效;结构';及空气污染指数,ios,swift,Ios,Swift,我有两组结构,我想通过API下载当前天气和天气预报数据,但有些结构具有相同的名称,然后Xcode通知我: “Main”的重新声明无效 这意味着我有同名的结构。 如何在Swift中更改结构的名称以消除此错误,并且不会导致数据下载问题 当前: import Foundation struct CurrentWeather : Decodable { let coord : Coordinate let cod, visibility, id : Int let name : String let

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

“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
 }

您需要更改名称,因为您的项目中似乎有相同的名称

struct MainJson : Decodable {
 let temp, tempMin, tempMax : Double
 let pressure, humidity : Int
}
然后用新名称替换相关密钥,如

let main : MainJson
这不会影响任何有效名称对json结构/类名的解码,重要的部分是键名

let main << this : Main << not this

let main您可以编写以下天气预报模型。目前的天气模式没有变化

////////////////////
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
}