Hello,
To learn Swift I am using a book "Develop in Swift: Data Collections". In the Unit 2: Working with the Web, in the Guided Project – Restaurant I have an issue.
Open Restaurant.app creates a server, which stores some images and then creating URLs for them. In Xcode after fetching images I can see those URLs "http://localhost:8080/images/1.png", but app doesn't display them (folder with images is not empty)! When I put this link in Safari, it shows me an error {"error":true,"reason":"The menu item with ID 1.png does not exist.}"
I also tried using Teacher's Guide solution and nothing changed.
I will be glad of your help!
Here's how i fetch images
class MenuController {
func fetchImage(url: URL, completion: @escaping (UIImage?) -> Void) {
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
if let data = data, let image = UIImage(data: data) {
completion(image)
} else {
completion(nil)
}
}
task.resume()
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MenuItem", for: indexPath)
configureCell(cell, forItemAt: indexPath)
return cell
}
func configureCell(_ cell: UITableViewCell, forItemAt indexPath: IndexPath) {
let menuItem = menuItems[indexPath.row]
cell.textLabel?.text = menuItem.name
cell.detailTextLabel?.text = MenuController.priceFormatter.string(from: NSNumber(value: menuItem.price))
MenuController.shared.fetchImage(url: menuItem.imageURL) { (image) in
guard let image = image else { return }
DispatchQueue.main.async {
if let currentIndexPath = self.tableView.indexPath(for: cell), currentIndexPath != indexPath {
return
}
cell.imageView?.image = image
cell.setNeedsLayout()
}
}
}