Guided Project – Restaurant, Images don'r display

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()
            }
        }
    }

I get the same bug

Same problem for me. Does anyone have a solution for this?

Same over here. I added a few print() statements to the fetchImage function to see if I could get a slight idea of what was going on. func fetchImage(url: URL, completion: @escaping (UIImage?) -> Void) {

        let task = URLSession.shared.dataTask(with: url) {

            (data, response, error) in

            if let data = data {

                print("data: (data)")

                if let image = UIImage(data: data) {

                completion(image)

               } else {

                   print("Could not fetch image")

               }

            } else {

                print("Failed to fetch data")

                completion(nil)

            }

        }

        task.resume()

    }

Here is my consul output: data: 69 bytes Could not fetch image

As far as my understanding goes right now, I assume the data for the image is retrieved but when looking for the image with the data it can't access the images folder so it can't find the requested data?

Hello, I get the same bug. I understood that the app answer to localhost:8080/images/menu Id When you get the imageURL, you get the string localhost:8080/images/.png

So I fix the problem by replacing ".png" by "" fetchImage

.....
let urlString = url.absoluteString.replacingOccurrences(of: ".png", with: "")
let newURL = URL(string: urlString)!

 let task = URLSession.shared.dataTask(with: newURL, completionHandler: {
            (data, response, error) in
.....

But I'm stuck now in a network error for retrieving the image

2022-10-08 11:30:00.679214+0200 OrderApp[18210:1105466] [connection] nw_socket_handle_socket_event [C3.1:2] Socket SO_ERROR [61: Connection refused]

I noticed that when I try http://localhost:8080/images/3 in Firefox (Safari doesn't accept http), it works only one time after a stop/start on the Open restaurant App

HTTP/5.0 200 OK
Connection: keep-alive
Content-Type: image/png
Content-Length: 129526
connection: keep-alive
date: Sat, 08 Oct 2022 09:09:48 GMT

‰PNG

I wish I could finish this lesson :) !

Guided Project – Restaurant, Images don'r display
 
 
Q