WWDC24 SALE: Save 50% on all my Swift books and bundles! >>

Getting errors when building AsyncImage example from SwiftUI tutorials

Forums > SwiftUI

@rvic  

Hello, cannot compile next simple example on Xcode 13.4.1

import SwiftUI

struct ContentView: View {
    var body: some View {
            AsyncImage(url: URL(string: "https://hws.dev/img/logo1.png"), scale: 3) { phase in
                if let image = phase.image {
                    image
                        .resizable()
                        .scaledToFit()
                }
                else if phase.error != nil {
                    Text("There was an eror loading image.")
                }
                else {
                    ProgressView()
                }
            } placeholder: {
                ProgressView()
            }
            .frame(width: 200, height: 200)
    }
}

Errors are:

  1. Value of type 'Image' has no member 'image' for line "if let image = phase.image {"
  2. Value of type 'Image' has no member 'error' for line "else if phase.error != nil {"

Please help.

2      

    else {
        ProgressView()
    }
} placeholder: {
     ProgressView()
}

The else part is already a placeholder for the AsyncImage, so the placeHolder: is unnecessary, so delete it.

else {
    ProgressView()
}

Then you'll hit the next problem, as the URL optional - does it even have an image?

2      

@rvic  

Thank you a lot for helping. For some reason, Xcode doesn't show quick help for init when iOS simulator is not running. Now i can see that reason for error is that version of AsyncImage init with closure having AsyncImagePhase instance doesn't have placeholder argument.

About next problem: URL value was intentionally set to wrong one to test case of missing image . Correct image is located at https://hws.dev/img/logo.png

2      

Save 50% in my WWDC sale.

SAVE 50% To celebrate WWDC24, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.