UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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.