GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

swiftData: Why it is always showing my noPreviewView" of "image not available" ?

Forums > SwiftUI

I want my noPreviewView it to appear just when there is no smallThumbnail available, but the noPreviewView always appears...

It is not with != nil ????

If I delete the "if" ... "else", and just leave the asyncImage it works perfect... so it must be something about the != nil I think...

HStack {

                        if book.volumeInfo.imageLinks?.smallThumbnail != nil {
                            NoPreviewView()
                                .frame(width: 130)
                                .scaledToFit()
                        } else {
                            AsyncImage(url: URL(string: (book.volumeInfo.imageLinks?.smallThumbnail)!)) { image in
                                image
                                    .resizable()
                                    .scaledToFit()
                                    .clipShape(.rect(cornerRadius: 15))
                            } placeholder: {
                                ProgressView()
                            }
                            .frame(width: 100, height: 200)
                        }

                        Text (book.volumeInfo.title)
                    }

   

@MattX  

You said that you want the NoPreviewView to show when there is no smallThumbnail unavailable... Perhaps you have the logic backwards? Try switching != nil to == nil...

1      

You would be better to do a if let then you would not have to force unwrap the URL - safer.

if let url = URL(string: (book.volumeInfo.imageLinks?.smallThumbnail) {
  AsyncImage(url: url) // etc
} else {
  // no preview
}

   

Hi @NigelGee !!!!

I am trying your advice, but It forces me to unwrap it... what am I doing wrong? it says:

Value of optional type 'String?' must be unwrapped to a value of type 'String'

Insert '!'

if let url = URL(string: (book.volumeInfo.imageLinks?.smallThumbnail)) {
                            AsyncImage(url: url) { image in
                                image
                                    .resizable
                                    .scaledToFit()
                                    .clipShape(.rect(cornerRadius: 15))
                            } placeholder: {
                                Image("placeholder")
                                    .resizable()
                                    .scaledToFill()
                            }
                            .frame(width: 100, height: 200)

                            Text (book.volumeInfo.title)

                        } else {
                            NoPreviewView()
                        }

   

Try

if let urlString = book.volumeInfo.imageLinks?.smallThumbnail, url = URL(string: urlString) {
  AsyncImage(url: url) { image in
      image
          .resizable
          .scaledToFit()
          .clipShape(.rect(cornerRadius: 15))
  } placeholder: {
      Image("placeholder")
          .resizable()
          .scaledToFill()
  }
  .frame(width: 100, height: 200)

  Text (book.volumeInfo.title)

} else {
  NoPreviewView()
}

   

@NigelGee If I try that, it shows error there and also in the foreach...

add image

   

As I do not have the code, think I missed a let before let url = URL(string: urlString)

Try removing the optional chain book.volumeInfo.imageLinks.smallThumbnail

   

Hacking with Swift is sponsored by Alex.

SPONSORED Alex is the iOS & Mac developer’s ultimate AI assistant. It integrates with Xcode, offering a best-in-class Swift coding agent. Generate modern SwiftUI from images. Fast-apply suggestions from Claude 3.5 Sonnet, o3-mini, and DeepSeek R1. Autofix Swift 6 errors and warnings. And so much more. Start your 7-day free trial today!

Try for free!

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

Reply to this topic…

You need to create an account or log in to reply.

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.