What I want to do can be represented as follows:

+2011 +2012 +2013 -2014 +1 +2 +3 -4 "maxTemp":"23.4" "minTemp":"3.1" "sunnyDays":"12" 

Years and months of type String.

I have a basic understanding of arrays and dictionaries, but I'm still a beginner, and this design is still complicated for me. I have a hunch how to create it, but I do not know how to add data at different levels later. var data = [[[String:String]]]() or

 var data = Array<Array<Dictionary<String, String>>>() 

How to write this in Swift3? And then how to add a new year, month and data to this design?

Thank you very much!

    1 answer 1

    For starters, I would create an object

     class Weather { var maxTemp: Double! var minTemp: Double! var sunnyDays: Int! init (_ maxTemp: Double, minTemp: Double, sunnyDays: Int) { self.maxTemp = maxTemp self.minTemp = minTemp self.sunnyDays = sunnyDays } } 

    Next, I would create a dictionary in which I would store all the data.

     var data: [String: Any] = [:] 

    the key to the data would be in the format yyyy-m

    would write data like this

     data["2014-4"] = Weather(23.4, 3.1, 12) 

    And then I would list all the years, within the years of the month, and refer to the data for the year + month, since they are known to us.

    If you wish, you can store months within years, and within months already given:

     var data: [String: [Weather]] = [:] data:["2014"].append[Weather(2.4, -13.1, 1)] //January data["2014"].append[Weather(5.4, -2.1, 3)] //February data["2014"].append[Weather(10.4, 3.1, 6)] //March data["2014"].append[Weather(15.4, 3.1, 12)] //April data["2014"].append[Weather(23.4, 3.1, 12)] //May 

    The list of years can be made manually

     var yearsList: [Int] = [] for y in 2010...2017 { yearsList.append(y) } 

    Months within the same year, it is always a range of 0 ... 11. In general, at the entrance you will probably have JSON with data in which there will be both a year and a month, you just need to write all this into the structure prepared above.

    • one
      Did a little differently, but your answer really helped. Thank! Sorry to reply late. - Robert