//
// HomeViewController.swift
// JsonText
//
// Created by Mohammad Azmal Hossain on 2/15/20.
// Copyright © 2020 Mohammad Azmal Hossain. All rights reserved.
//
import Foundation
import UIKit
/*
[
{
"id":6,
"name":"AppStore JSON APIs",
"link":"https://www.letsbuildthatapp.com/course/AppStore-JSON-APIs",
"imageUrl":"https://letsbuildthatapp-videos.s3-us-west-2.amazonaws.com/fd78be85-b845-4672-a316-7df0abe80812_medium",
"number_of_lessons":47
},
{
"id":5,
"name":"Tinder Firestore Swipe and Match",
"link":"https://www.letsbuildthatapp.com/course/Tinder-Firestore-Swipe-and-Match",
"imageUrl":"https://letsbuildthatapp-videos.s3-us-west-2.amazonaws.com/bc5a4091-d2ea-44ab-b749-5f2ee5354c35_medium",
"number_of_lessons":47
},
{
"id":1,
"name":"Instagram Firebase",
"link":"https://www.letsbuildthatapp.com/course/instagram-firebase",
"imageUrl":"https://letsbuildthatapp-videos.s3-us-west-2.amazonaws.com/04782e30-d72a-4917-9d7a-c862226e0a93",
"number_of_lessons":49
},
{
"id":2,
"name":"Podcasts Course",
"link":"https://www.letsbuildthatapp.com/course/podcasts",
"imageUrl":"https://letsbuildthatapp-videos.s3-us-west-2.amazonaws.com/32f98d9d-5b9b-4a22-a012-6b87fd7158c2_medium",
"number_of_lessons":39
},
{
"id":3,
"name":"Intermediate Training Core Data",
"link":"https://www.letsbuildthatapp.com/course/intermediate-training-core-data",
"imageUrl":"https://letsbuildthatapp-videos.s3-us-west-2.amazonaws.com/0736fecb-5b88-483b-a83d-ca2a5a6d93f9_medium",
"number_of_lessons":32
},
{
"id":4,
"name":"Kindle Basic Training",
"link":"https://www.letsbuildthatapp.com/basic-training",
"imageUrl":"https://letsbuildthatapp-videos.s3-us-west-2.amazonaws.com/a6180731-c077-46e7-88d5-4900514e06cf_medium",
"number_of_lessons":19
}
]*/
// MARK: - Courses
struct CoursesM: Codable {
let id: Int?
let name: String?
let link, imageURL: String?
let numberOfLessons: Int?
enum CodingKeys: String, CodingKey {
case id, name, link
case imageURL = "imageUrl"
case numberOfLessons = "number_of_lessons"
}
}
class HomeViewController: UIViewController {
var courses = [CoursesM]()
@IBOutlet weak var homeTableView: UITableView!
func getRequestApiForCourse(){
let stringUrl = "http://api.letsbuildthatapp.com/jsondecodable/courses"
let urlObj = URL(string: stringUrl)
guard let urlUnWrap = urlObj else {
return
}
let task = URLSession.shared.dataTask(with: urlUnWrap){(data, response, error) in
guard let dataG = data else {
return
}
do{
let json = try JSONDecoder().decode([CoursesM].self, from: dataG)
print("azmal json = \(json)")
for jsons in json{
self.courses.append(jsons)
print("azmal jsons = \(jsons)")
}
} catch let errorJson {
print("azmal error = \(errorJson)")
}
DispatchQueue.main.async {
self.homeTableView.reloadData()
}
}
task.resume()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
getRequestApiForCourse()
setup()
}
func setup(){
let nib = UINib(nibName: "HomeTableViewCell", bundle: nil)
homeTableView.register(nib, forCellReuseIdentifier: "HomeTableViewCell")
homeTableView.delegate = self
homeTableView.dataSource = self
}
}
extension HomeViewController: UITableViewDelegate, UITableViewDataSource{
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return courses.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "HomeTableViewCell", for: indexPath) as! HomeTableViewCell
cell.titleLabel.text = self.courses[indexPath.row].name
return cell
}
}
No comments:
Post a Comment