Reputation: 613
I am getting a response and I want to get only image URL from whole data and populate in collection view first let me show you my code
CODE
func callSubChapAPI(){
let preferences = UserDefaults.standard
let studentlvl = "student_lvl"
let student_lvl = preferences.object(forKey: studentlvl) as! String
print(student_lvl)
let params = ["level_id": student_lvl]
Alamofire.request(subListWithChapter, method: .post, parameters: params).responseData() { (response) in
switch response.result {
case .success(let data):
do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let subjects = try decoder.decode(SubjectResponse.self, from: data)
let urls = subjects.subjectList.map({ $0.subList.map({ $0.chImage}) }).reduce([], +)
print(urls)
print(subjects)
} catch {
print(error.localizedDescription)
}
case .failure(let error):
print(error.localizedDescription)
}
}
}
here is my struct
struct SubjectResponse: Decodable {
let subjectList: [Subject]
}
struct Subject: Decodable {
let subList: [Chapter]
}
struct Chapter: Decodable {
let chId : String
let chImage: String
let chName: String
let conId: String
let levelId: String
let subId: String
}
so now I want chImage URL and set to collectionview
Here is The response
{
"subject_list" = (
{
"con_id" = 2;
"level_id" = 1;
"sub_id" = 4;
"sub_list" = (
{
"ch_id" = 17;
"ch_image" = "http://mobileapp.xmeducation.com/upload/1530600693.jpg";
"ch_name" = " 01. Measurement";
"con_id" = 2;
"level_id" = 1;
"sub_id" = 4;
},
{
"ch_id" = 23;
"ch_image" = "http://mobileapp.xmeducation.com/upload/1451930609.jpg";
"ch_name" = "1. Test Chapter";
"con_id" = 2;
"level_id" = 1;
"sub_id" = 4;
},
{
"ch_id" = 24;
"ch_image" = "http://mobileapp.xmeducation.com/upload/1884777188.jpg";
"ch_name" = "1. Test Chapter";
"con_id" = 2;
"level_id" = 1;
"sub_id" = 4;
},
{
"ch_id" = 25;
"ch_image" = "http://mobileapp.xmeducation.com/upload/1518702048.jpg";
"ch_name" = "1. Test Chapter";
"con_id" = 2;
"level_id" = 1;
"sub_id" = 4;
}
);
"sub_name" = Physics;
},
{
"con_id" = 2;
"level_id" = 1;
"sub_id" = 8;
"sub_list" = (
{
"ch_id" = 26;
"ch_image" = "http://mobileapp.xmeducation.com/upload/1437196139.jpg";
"ch_name" = " 1. Test Chapter";
"con_id" = 2;
"level_id" = 1;
"sub_id" = 8;
},
{
"ch_id" = 27;
"ch_image" = "http://mobileapp.xmeducation.com/upload/1903171865.jpg";
"ch_name" = "1. Test Chapter";
"con_id" = 2;
"level_id" = 1;
"sub_id" = 8;
}
);
"sub_name" = Chemistry;
},
{
"con_id" = 2;
"level_id" = 1;
"sub_id" = 9;
"sub_list" = (
{
"ch_id" = 31;
"ch_image" = "http://mobileapp.xmeducation.com/upload/1319333294.jpg";
"ch_name" = "1. Test Chapter";
"con_id" = 2;
"level_id" = 1;
"sub_id" = 9;
}
);
"sub_name" = Testing;
},
{
"con_id" = 2;
"level_id" = 1;
"sub_id" = 10;
"sub_list" = (
{
"ch_id" = 28;
"ch_image" = "http://mobileapp.xmeducation.com/upload/1373218664.jpg";
"ch_name" = "1. Test Chapter";
"con_id" = 2;
"level_id" = 1;
"sub_id" = 10;
}
);
"sub_name" = "Test Subject";
},
{
"con_id" = 2;
"level_id" = 1;
"sub_id" = 11;
"sub_list" = (
{
"ch_id" = 29;
"ch_image" = "http://mobileapp.xmeducation.com/upload/246189282.jpg";
"ch_name" = "1. Test Chapter";
"con_id" = 2;
"level_id" = 1;
"sub_id" = 11;
}
);
"sub_name" = "Test Subject 1";
},
{
"con_id" = 2;
"level_id" = 1;
"sub_id" = 12;
"sub_list" = (
{
"ch_id" = 30;
"ch_image" = "http://mobileapp.xmeducation.com/upload/1342731807.jpg";
"ch_name" = "1. Test Chapter";
"con_id" = 2;
"level_id" = 1;
"sub_id" = 12;
}
);
"sub_name" = "Test Subject 2";
}
);
so please tell me that how to get ch_image URL so, I just want to know that From SubjectResponse how can I get ch_image URL
Upvotes: 0
Views: 67
Reputation: 2209
So my answer is :
let subjects: SubjectResponse?
then:
self.subjects = try decoder.decode(SubjectResponse.self, from: data)
func numberOfSections(in collectionView: UICollectionView) -> Int {
return self.subjects.subjectList.count
}
func collectionView(_ collectionView: UICollectionView,
numberOfItemsInSection section: Int) -> Int {
return self.subjects.subjectList[section].count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt
indexPath: IndexPath) -> UICollectionViewCell {
var url = subject.subjectList[IndexPath.section].sublist[IndexPath.row].chImage
}
Upvotes: 0
Reputation: 2470
Please find below code. set numberOfSections
count to subjectList
's count. and for each section set numberOfItemsInSection
to subjectList[section]
's count. and in the end get your image url in cellForItemAt
method and set on the image.
func numberOfSections(in collectionView: UICollectionView) -> Int {
return subjectList.count
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return subjectList[section].count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
// Dequeue your cell here and set the image from
// let image_url = subjectList[IndexPath.section][IndexPath.row].ch_image
}
for getting data into subjectList
. define subjectList
variable on top
var subjectList : [Subject]!
in your success case of Alamofire :
case .success(let data):
do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let subjects = try decoder.decode(SubjectResponse.self, from: data)
self.subjectList = subjects.subjectList
collectionView.reloadData()
Upvotes: 1