BLACK FRIDAY 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.

   

    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?

   

@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

   

Hacking with Swift is sponsored by Guardsquare

SPONSORED AppSweep by Guardsquare helps developers automate the mobile app security testing process with fast, free scans. By using AppSweep’s actionable recommendations, developers can improve the security posture of their apps in accordance with security standards like OWASP.

Learn more

Sponsor Hacking with Swift and reach the world's largest Swift community!

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.