I’m a beginner. I pull out pictures in cells through api. Everything is built, but instead of pictures – it’s empty. Returns nil. I’ve been sitting here all day and can’t figure it out!
API link – https://swiftbook.ru//wp-content/uploads/api/api_courses
If this answer is somewhere, I apologize, and if it’s not difficult to give a link, send it please, thank you.
Thank you very much in advance for your help and clarification!!!
import UIKit
class CourseCell: UITableViewCell {
@IBOutlet var courseImage: UIImageView!
@IBOutlet var courseNameLabel: UILabel!
@IBOutlet var numberOfLessons: UILabel!
@IBOutlet var numberOfTests: UILabel!
func configure(with course: Course) {
courseNameLabel.text = course.name
numberOfLessons.text = "Number of lessons (course.number_of_lessons ?? 0)"
numberOfTests.text = "Number of tests (course.number_of_tests ?? 0)"
DispatchQueue.global().async {
guard let stringUrl = course.imageUrl,
let imageURL = URL(string: stringUrl),
let imageData = try? Data(contentsOf: imageURL)
else {
return
}
DispatchQueue.main.async {
self.courseImage.image = UIImage(data: imageData)
}
}
}
}
Model for decode by JSON
Course.swift
struct Course: Decodable {
let name: String?
let imageUrl: String?
let number_of_lessons: Int?
let number_of_tests: Int?
}
struct WebsiteDescription: Decodable {
let courses: [Course]?
let websiteDescription: String?
let websiteName: String?
}
And piece of code with JSON from CoursesViewController.swift
extension CoursesViewController {
func fetchCourses() {
guard let url = URL(string: URLExamples.exampleTwo.rawValue) else { return }
URLSession.shared.dataTask(with: url) { (data, _, _) in
guard let data = data else {
return
}
do {
// получаем курсы в переменную
self.courses = try JSONDecoder().decode([Course].self, from: data)
// и мы должны перезагрузить таблицу
DispatchQueue.main.async {
self.tableView.reloadData()
}
} catch let error {
print(error)
}
}.resume()
}
}
And here is i get nil probably (please see screenshot below)
2
Answers
The answer above is Excellent !!! It's an additional valuable experience for me!
But main reason was in blocked .ru domains in my country. WHEN I ACCIDENTALLY TURNED ON THE VPN ON THE MAC AND BUILD APP, THEN EVERYTHING LOADED!!! Because I am in Ukraine now, and we have all .ru domains blocked, and the API URL is just on .ru
I tried to make another version of your code and it’s able to run. You can check my code and compare with your own.
CoursesViewController
Cell
Some Improvement suggestion
Course.swift
: Please use lower camel case convention for variables name because it’s the common Swift conventionCourseCell.swift
: Since the course don’t have ID so after a while you load image from background, this cell might be used by another because of the reuse cell mechanism.SDWebImage
orKingFisher
.