I want to show section title as January,February,March(as per data comes from api) instead of showing January, March, February.
I am getting this data from backend api.
wholeDic : NSDictionary = {
January = (
{
date = "20-jan-18";
subtitle = "Only four days remaining";
title = "School fees of August, 2018School fees of August, 2018";
},
{
date = "21-jan-18";
subtitle = Holi;
title = "Holiday on third march";
}
);
february = (
{
date = "20-feb-18";
subtitle = "Paricipate in this activity";
title = "Annual function will be held in feb,2018";
},
{
date = "20-12-18";
subtitle = "Holiday issue by Govt. of India";
title = "Bharat Band";
}
);
march = (
{
date = "20-feb-18";
subtitle = "Paricipate in this activity";
title = "Annual function will be held in feb,2018";
},
{
date = "20-feb-18";
subtitle = "Paricipate in this activity";
title = "Annual function will be held in feb,2018";
}
);}
Now I have fetched "key" and added in array
for (key, value) in wholeDic{
sectionTitleArray.add(key)
print(sectionTitleArray)
}
and when I print sectionTitleArray than console is showing January, March, February
instead of showing January, February, March
.
I know that Dictionary is an unordered collection but I want to know that how to fetch keys by order?
My UitableView DataSource & Delegate are
func numberOfSections(in tableView: UITableView) -> Int{
return sectionTitleArray.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let sectionTitle : String = sectionTitleArray.object(at: section) as! String
let sectiondes:NSArray = wholeDic.object(forKey: sectionTitle) as! NSArray
return sectiondes.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "cell"
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! frameNboundTableViewCell
let sectionTitle : String = sectionTitleArray.object(at: indexPath.section) as! String
let contentArr : NSArray = wholeDic.object(forKey: sectionTitle) as! NSArray
let contentDic : NSDictionary = contentArr.object(at: indexPath.row) as! NSDictionary
print(contentDic)
cell.titleLbl.text = contentDic.value(forKey: "title") as? String
cell.titleLbl.numberOfLines = 0
return cell
}
Based on the comments you can change the api response into array and simply access with index. The json response will look like this
{
"data" : [
{
"month" : "January",
"details" : [
{
"date" : "20-jan-18",
"subtitle" : "Only four days remaining",
"title" : "School fees of August, 2018School fees of August, 2018"
},
{
"date" : "21-jan-18",
"subtitle" : "Holi",
"title" : "Holiday on third march"
}
]
},
{
"month" : "february",
"details" : [
{
"date" : "20-feb-18",
"subtitle" : "Paricipate in this activity",
"title" : "Annual function will be held in feb,2018"
},
{
"date" : "20-12-18",
"subtitle" : "Holiday issue by Govt. of India",
"title" : "Bharat Band"
}
]
},
{
"month" : "march",
"details" : [
{
"date" : "20-feb-18",
"subtitle" : "Paricipate in this activity",
"title" : "Annual function will be held in feb,2018"
},
{
"date" : "20-feb-18",
"subtitle" : "Paricipate in this activity",
"title" : "Annual function will be held in feb,2018"
}
]
}
]
}
Now you can access the first object from the json array and then retrieve the respective key and display it. It will be something like
let object = response.data![index]
object.month // "January"
Hope it helps