I got json from API and put it in array with code similar to the one below.
SWIFT Code
var movieList: [[String:AnyObject]]=[ ]
func sample(){
Alamofire.request (.GET, "http://123456789").validate().responseJSON {response in
switch response.result {
case.Success:
iflet value = response.result.value {
let json=JSON(value)
for (key,_): (String, JSON) in json ["movies"] {
for (key2,_): (String, JSON) in json ["movies"] [key] {
for(_,subJson3): (String,JSON) in json ["movies"] [key][key2] {
let movieList: String: AnyObject = [
"id": subJson3["id"].int!,
"title": subJson3["title"].string!,
"published_year": subJson3["published_year"].string!
]
self.movieList.append(movieList)
}
}
}
self.tableView.reloadData()
}
case.Failure:
print("Failed to get information.")
}
}
}
JSON data
{
"movies": {
"2015" : {
"1" : [
{
"id": 111,
title: Title 1,
"published_year": "2015",
}
],
"6" : [
{
"id": 222,
title: Title 2,
"published_year": "2015",
}
]
},
"2008" : {
"3" : [
{
"id"—333,
title: Title 3,
"published_year": "2008",
},
{
"id" : 444,
title: Title 4 ,
"published_year": "2008",
}
],
},
"2016" : {
"1" : [
{
"id" : 555,
title: Title 5 ,
"published_year": "2016",
},
{
"id" : 666,
title: Title 6 ,
"published_year": "2016",
}
]
}
}
}
What's in the movieList array data
var movieList: [[String: AnyObject]] = [["id": 111, "title": タイトル1, "published_year" : 2015], ["id": 222, "title": タイトル2, "published_year" : 2015], ["id": 333, "title": タイトル3, "published_year" : 2008], ["id": 444, "title": タイトル4, "published_year" : 2008], ["id": 555, "title": タイトル5, "published_year" : 2016], ["id": 666, "title": タイトル6, "published_year" : 2016]]
Is it possible to divide tableView into annual sections from this data and store the title in row?
In short, I would like to make it look like the image below.
Or should I make a dictionary like the one below?
var movieList: [String: AnyObject] = [
2015:[[id]:111, "title": Title 1, "published_year":2015], [id":222, "title": Title 2, "published_year":2015]], 2008:[[id":333, "title": Title 3, "published_year":2008], [id":444, "title":4, "published_year":2016]
]
What kind of array or dictionary type should I create to create a tableView like the attached image?We have data for about 100 years, so we would like to avoid creating variables for 100 years.
swift ios
This is what it looks like to express the JSON data you presented in Swift data type.
let jsonObject:[String:[String:[[String:AnyObject]]]]=[
"movies": [
"2015" : [
"1" : [
[
"id": 111,
title: Title 1,
"published_year": "2015",
]
//,...
]
//,...
//,...
]
]
It's so complicated that I don't feel like using it as it is.
But two examples you wrote:
var movieList: [[String:AnyObject]]
I have to reconfigure the section information every time.
var movieList: [String: AnyObject]
For the , see
var movieList: [String:[String:AnyObject]]]
I think it's a mistake to write in , but the outermost data type Dictionary
is out of order, so you have to rearrange the data so that it's in the order you want to display every time.Just as the data types shown were incorrect, as the hierarchy deepens to this level, it is easy to lose track of which tier and which data you are accessing.
This may be contrary to the title "best array or dictionary type", but overall code can be simpler if you define the best data structure for what your app is trying to do.
It may sound a little confusing, but I'll prepare two data types:
structure Movie:CustomStringConvertible{
variable —Int
variable —String
var publishedYear: String
init(json movieItem:JSON){
self.id=movieItem["id"].int!
self.title=movieItem["title"].string!
self.publishedYear=movieItem["published_year"].string!
}
// For debugging
var description: String {
return "<Movie:\(id):\(title)(\(publishedYear))>"
}
}
structureMovieSection:CustomStringConvertible{
var header:String
var movies —Movies
init(header:String, json yearlyMovies:JSON) {
self.header=header
var movies: Movie = [ ]
for(_,monthlyMovies) in yearlyMovies {
for(_,movie) in monthlyMovies {
movies.append (Movie(json:movie))
}
}
self.movies=movies
}
// For debugging
var description: String {
return "<MovieSection:\(header)\n\(movies))>"
}
}
Use the data type above to prepare the instance variables.
var movieSections: MovieSection = [ ]
Then you can write the part where you want to put the data in this array like this
case.Success(let value):
let json=JSON(value)
for (year, section) in json ["movies"].sort({$0.0>$1.0}){
self.movieSections.append(MovieSection(header:"\"(year)year", json:section))
}
What do you think?Don't you think it's going to be quite refreshing?
In order to realize the UITableView
screen you presented, you need to implement the following four methods of UITableViewDataSource
, which is also quite refreshing.
func numberOfSectionsInTableView (tableView:UITableView) - > Int{
return movieSections.count
}
functableView (tableView: UITableView, numberOfRowsInSection: Int) - > Int {
return movieSections [section].movies.count
}
functableView (tableView: UITableView, titleForHeaderInSection section: Int) - > String?{
return movieSections [section].header
}
functableView(tableView:UITableView, cellForRowAtIndexPathindexPath:NSIndexPath) - >UITableViewCell{
letcell=tableView.dueReusableCellWithIdentifier("Cell", forIndexPath:indexPath)
let movie=movieSections [indexPath.section].movies [indexPath.row]
cell.textLabel?.text=movie.title
return cell
}
While Swift's Dictionary
equivalent data structures are easy to express, they are often difficult to use when used for long-lasting or frequently referenced data.
By defining the best data structure for the problem area, the entire program can be refreshed.Please try it.
613 GDB gets version error when attempting to debug with the Presense SDK (IDE)
578 Understanding How to Configure Google API Key
573 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
916 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
618 Uncaught (inpromise) Error on Electron: An object could not be cloned
© 2024 OneMinuteCode. All rights reserved.