Ios 将项追加到数组将覆盖以前的项并添加新项

Ios 将项追加到数组将覆盖以前的项并添加新项,ios,arrays,swift,Ios,Arrays,Swift,我在将项附加到数组时遇到问题。我正在使用Alamofire和开放天气API来获取这些项目 获取JSON响应并解析它不是问题。将第一项添加到数组中也可以正常工作 项目示例: 日期:星期一高温:85F低温: 72F天气类型:雨 日期:星期二高温:102F低温: 97F天气类型:晴天 日期:星期三高温:98F低温: 62F天气类型:雨 日期:星期四高温:95F低温: 92F天气类型:多云 日期:星期五高温:83F低温: 77F天气类型:晴天 日期:星期六高温:100华氏度低温:85华氏度天气类型:晴朗

我在将项附加到数组时遇到问题。我正在使用Alamofire和开放天气API来获取这些项目

获取JSON响应并解析它不是问题。将第一项添加到数组中也可以正常工作

项目示例:

日期:星期一高温:85F低温: 72F
天气类型:雨

日期:星期二高温:102F低温: 97F
天气类型:晴天

日期:星期三高温:98F低温: 62F
天气类型:雨

日期:星期四高温:95F低温: 92F
天气类型:多云

日期:星期五高温:83F低温: 77F
天气类型:晴天

日期:星期六高温:100华氏度低温:85华氏度天气类型:晴朗

这些数据存储在WeatherForecast对象中,该对象依次添加/附加到WeatherForecast数组中

问题是,最终发生的是本例中的最后一个值 星期六将填充阵列中的所有六个元素

使用for循环,我注意到它是这样的

正确添加第一项,然后添加第二项两次,覆盖第一项。然后第三项覆盖第一项和第二项。。。重复直到结束

使用数字会发生什么: 1. 22 333 4444 55555 666666(无效) { 请求(getForecastURL) A.responseJSON先生 { 回应 让result=response.result; 如果让dict=result.value作为字典 { 如果let list=dict[“list”]as?[词典] { 对于列表中的术语 { 如果让temp=dictItem[“temp”]作为字典 { 如果让maxTemp=temp[“max”] { self.weatherForecast.setHighTemp(高温:self.convertToString(温度:maxTemp)); } 如果让minTemp=temp[“min”] { self.weatherForecast.setLowTemp(低温:self.convertToString(温度:minTemp)); } } 如果让weatherArray=dictItem[“weather”]as? [字典] { 如果让main=weatherArray[0][“main”]作为字符串 { self.weatherForecast.setWeatherType(weatherType:main.capitalized); } } 如果let date=dictItem[“dt”]为双精度 { 让convertedDate=日期(时间间隔自1970年起:日期); self.weatherForecast.setDate(日期:convertedDate.dayOfWeek()!); } 自计数器+=1; self.arrayFints.append(self.counter); self.weatherForecast.append(self.weatherForecast); } //int数组工作正常。 对于self.arrayFints中的整数 { 打印(“Int val是:\(整数)”; } } 已完成(自检索成功(weatherForecasts:self.weatherForecasts)); } 其他的 { 已完成(自我检索失败(消息:“六天预测错误”); } } } }
我尝试使用ArrayFints使用append,效果很好,没有任何覆盖问题让我无法理解我的WeatherForecasts数组有什么问题。

每次迭代数组时,都需要创建新的WeatherForecast对象

class WeatherService {
    private var weatherForecasts: [WeatherForecast] = [WeatherForecast]()

    public func downloadWeatherForecast(getForecastURL: String, completed: @escaping(_ weatherPayload: WeatherPayload) -> Void) {
        Alamofire.request(getForecastURL).responseJSON { response in
            if let dict = response.result.value as? Dictionary<String, AnyObject> {
                if let list = dict["list"] as? [Dictionary<String, AnyObject>] {
                    for dictItem in list {
                        var weatherForecast = WeatherForecast()
                        if let temp = dictItem["temp"] as? Dictionary<String, Double> {
                            if let maxTemp = temp["max"] {
                                weatherForecast.setHighTemp(highTemp: self.convertToString(temp: maxTemp));
                            }

                            if let minTemp = temp["min"] {
                                weatherForecast.setLowTemp(lowTemp: self.convertToString(temp: minTemp));
                            }
                        }

                        if let weatherArray = dictItem["weather"] as? [Dictionary<String, AnyObject>] {
                            if let main = weatherArray[0]["main"] as? String {
                                weatherForecast.setWeatherType(weatherType: main.capitalized);
                            }
                        }

                        if let date = dictItem["dt"] as? Double {
                            let convertedDate = Date(timeIntervalSince1970: date);
                            weatherForecast.setDate(date: convertedDate.dayOfWeek()!);
                        }

                        self.arrayOfInts.append(self.counter);
                        self.weatherForecasts.append(weatherForecast);
                    }
                }
                completed(self.retrievalSuccess(weatherForecasts: self.weatherForecasts));
            } else {
                completed(self.retrievalFailure(message: "Six day forecast error."));
            }
        }
    }
}
class天气服务{
私人var WeatherForecast:[WeatherForecast]=[WeatherForecast]()
public func downloadWeatherForecast(getForecastURL:String,已完成:@escaping(weatherPayload:weatherPayload)->Void){
请求(getForecastURL).responseJSON{response in
如果让dict=response.result.value作为字典{
如果let list=dict[“list”]as?[词典]{
对于列表中的术语{
var weatherForecast=weatherForecast()
如果让temp=dictItem[“temp”]作为字典{
如果让maxTemp=temp[“max”]{
weatherForecast.setHighTemp(高温:自转换字符串(温度:maxTemp));
}
如果让minTemp=temp[“min”]{
weatherForecast.setLowTemp(低温:自转换字符串(温度:minTemp));
}
}
如果让weatherArray=dictItem[“weather”]作为?[字典]{
如果让main=weatherArray[0][“main”]作为字符串{
weatherForecast.setWeatherType(weatherType:main.大写);
}
}
如果let date=dictItem[“dt”]为双精度{
让convertedDate=日期(时间间隔自1970年起:日期);
weatherForecast.setDate(日期:convertedDate.dayOfWeek()!);
}
self.arrayFints.append(self.counter);
self.weatherForecast.append(weatherForecast);
}
class WeatherService {
    private var weatherForecasts: [WeatherForecast] = [WeatherForecast]()

    public func downloadWeatherForecast(getForecastURL: String, completed: @escaping(_ weatherPayload: WeatherPayload) -> Void) {
        Alamofire.request(getForecastURL).responseJSON { response in
            if let dict = response.result.value as? Dictionary<String, AnyObject> {
                if let list = dict["list"] as? [Dictionary<String, AnyObject>] {
                    for dictItem in list {
                        var weatherForecast = WeatherForecast()
                        if let temp = dictItem["temp"] as? Dictionary<String, Double> {
                            if let maxTemp = temp["max"] {
                                weatherForecast.setHighTemp(highTemp: self.convertToString(temp: maxTemp));
                            }

                            if let minTemp = temp["min"] {
                                weatherForecast.setLowTemp(lowTemp: self.convertToString(temp: minTemp));
                            }
                        }

                        if let weatherArray = dictItem["weather"] as? [Dictionary<String, AnyObject>] {
                            if let main = weatherArray[0]["main"] as? String {
                                weatherForecast.setWeatherType(weatherType: main.capitalized);
                            }
                        }

                        if let date = dictItem["dt"] as? Double {
                            let convertedDate = Date(timeIntervalSince1970: date);
                            weatherForecast.setDate(date: convertedDate.dayOfWeek()!);
                        }

                        self.arrayOfInts.append(self.counter);
                        self.weatherForecasts.append(weatherForecast);
                    }
                }
                completed(self.retrievalSuccess(weatherForecasts: self.weatherForecasts));
            } else {
                completed(self.retrievalFailure(message: "Six day forecast error."));
            }
        }
    }
}
X.day = "Monday"
forecasts.add(X)
//forecasts is now [X]
X.day = "Tuesday"
forecasts.add(X)
//forecasts is now [X,X]
X.day = "Wednesday"
forecasts.add(X)
//forecasts is now [X,X,X]
for dictItem in list {
for dictItem in list {
    weatherForecast = WeatherForecast()