SwiftUI中使用Image(song.image)报错Argument type 'String' does not conform to expected type 'Decoder'的问题咨询
提问详情
import SwiftUI import AVKit struct Song: Identifiable { let id = UUID() let title: String let artist: String let image: String let mp3FileName: String } struct MusicView: View { let songs: [Song] = [ Song(title: "Warriors", artist: "Imagine Dragons", image: "warriors", mp3FileName: "Warriors (ft. Imagine Dragons)"), // other songs ] var body: some View { ScrollView { VStack { ForEach(songs) { song in HStack { Image(song.image) // Error here Argument type 'String' does not conform to expected type 'Decoder .resizable() .scaledToFit() .frame(width: 80, height: 80) .cornerRadius(10) VStack(alignment: .leading) { Text(song.title) .font(.headline) Text(song.artist) .font(.subheadline) } Spacer() } .padding() Divider() } } } } }
What I tried:
- I checked that the image names in the assets match exactly with the images in my asset catalog.
- I confirmed that the images exist in the asset catalog.
Expected behavior:
I expect the images to be displayed next to the song titles and artists.
Error:Argument type 'String' does not conform to expected type 'Decoder' on Image(song.image)
我的回答
Hey there! That error looks like a classic naming conflict issue. Here's what's going on and how to fix it:
The compiler is getting confused about which Image type you're trying to use. Instead of picking SwiftUI's Image that takes a resource name string, it's matching a different Image type in your project (like a custom struct you made, a CoreData entity named Image, or even a type from another framework you imported) that only has a decoder initializer (for Codable).
Quick Fix: Explicitly use SwiftUI's Image
Change your Image(song.image) line to explicitly reference SwiftUI's Image type. That will cut through the naming confusion immediately:
SwiftUI.Image(song.image) .resizable() .scaledToFit() .frame(width: 80, height: 80) .cornerRadius(10)
Longer-Term Fix: Resolve the Naming Conflict
Take a quick look through your project to find where the other Image type is coming from:
- Did you create a custom struct/class named
Imagesomewhere? Rename it (likeSongImageorAppImage) to avoid clashes. - Do you have a CoreData entity called
Image? Give it a more specific name too. - Are you importing a framework that includes its own
Imagetype? You might need to keep using theSwiftUI.prefix for all your image views, or find a way to alias the types.
Double-Check the Basics (Just to Be Sure)
Since you already verified the asset names match, just a quick reminder: iOS asset catalogs are case-sensitive! Make sure "warriors" in your code exactly matches the case of the asset name (no uppercase letters where there shouldn't be any). Also, try cleaning your build folder (Product > Clean Build Folder) and re-running the app—sometimes Xcode's cache causes weird resource issues.
Once you make that change, your images should pop up next to your song info like you expect!
备注:内容来源于stack exchange,提问作者Yu-Jia Liang

