0

I am using an endpoint that returns a JSON as response. The problem is response json is a huge data to process for me. From that I want to show all the Surah(englishName) name to display in the tableview. I tried my best as a new bee to iOS development. Please take a look at my snippet and let me know where i am doing wrong.

my Json data here:

ViewController code:

var surahName = [Surah]()

@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
}


//MARK: JSON parse
func parseJSON()  {
    let url = URL(string: "https://api.alquran.cloud/v1/quran/ar.alafasy")
    
    guard url != nil else{
        print("URL Founr Nill")
        return
    }
    
    URLSession.shared.dataTask(with: url!) { (data, response, error) in
        if error == nil && data != nil{
            do{
                self.surahName = try JSONDecoder().decode([Surah].self, from: data!)

                DispatchQueue.main.async {
                    self.tableView.reloadData()
                }
            }catch{
                print(error)
            }
        }
    }.resume()
}

//MARK: Tableview delegate
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return surahName.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell:QuranAudioCell = tableView.dequeueReusableCell(withIdentifier: "cell") as! QuranAudioCell
    let arrdata = surahName[indexPath.section].data.surahs
    cell.nameLbl.text = arrdata[indexPath.row].englishName
    return cell
}

Problem is its not printing anything in the tablview.

1 Answer 1

1

Change first line as

var surahNames = [EnglishName]()

Inside do-catch block change

self.surahName = try JSONDecoder().decode([Surah].self, from: data!)

into

let response = try JSONDecoder().decode(Surah.self, from: data!)
self.surahName = response.data.surahs

Now inside cellForRowAtIndexPath do this

let surah = surahName[indexPath.row]
cell.nameLbl.text = surah.englishName
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you brother. Really appreciate your help. i find out you are from Bangladesh too. Nice to meet you. And i will be very happy and grateful to you if you share your LinkedIn profile with me, it will a great chance to follow you.
Welcome brother @Asifmimi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.