I'm playing around with SwiftUI, I could display an Image from the asset in my Home page but when I try to call it for a model of some french company I can't ...
import SwiftUI
struct Company: Identifiable {
var id = UUID()
var name: String
var logo: Image
var city: String
var endDate: String
var contact: String
var contractType: String
init(id: UUID = UUID(), name: String, logo: Image, city: String, endDate: String, contact: String, contractType: String) {
self.id = id
self.name = name
self.logo = logo
self.city = city
self.endDate = endDate
self.contact = contact
self.contractType = contractType
}
}
struct CardViewcontent: View {
let companyList = [
Company(name: "BBC", logo: Image("BBC"), city: "Boulogne", endDate: "00/00/00", contact: "01010101", contractType: "maintenance"),
Company(name: "Eliote", logo: Image("eliote"), city: "Boulogne", endDate: "00/00/00", contact: "01010101", contractType: "maintenance"),
Company(name: "Arthur World", logo: Image("AWPG"), city: "Boulogne", endDate: "00/00/00", contact: "01010101", contractType: "ponctuel"),
Company(name: "AKCB", logo: Image("AKCB"), city: "Paris", endDate: "00/00/00", contact: "01010101", contractType: "pas de contrat")
]
var body: some View {
VStack(spacing: 20) {
Text("List of Companies")
.font(.title)
.bold()
ForEach(companyList, id: \.name) { company in
CardView(company: company)
}
}
.padding()
}
}
struct CardView: View {
let company: Company
var body: some View {
VStack {
Text("\(company.name)")
.font(.title)
.bold()
.padding(.bottom)
Image("\(company.logo)")
.resizable()
.scaledToFit()
.padding()
Text("\(company.city)")
Text("\(company.contractType)")
Text("\(company.endDate)")
Text("\(company.contact)")
}
.padding()
.background(Color.white)
.cornerRadius(10)
.shadow(radius: 10)
.foregroundColor(.black)
}
}
The error:
No image named 'Image(provider: SwiftUI.ImageProviderBox<SwiftUI.Image.(unknown context at $1d4ea7490).NamedImageProvider>)' found in asset catalog for /Users/egage/Library/Developer/Xcode/DerivedData/NetoPark-gtrezayhmddlsdgrfbavwkdwplxf/Build/Products/Debug/NetoPark.app
this is my file no error on build and run it just don't display it, ( I already check if their names are the same from the assets and the code.
The logo
property of Company
is of type Image
. So, when you do Image("\(company.logo)")
, you are essentially doing Image("\(Image("BBC"))")
. When "\(Image("BBC"))"
is enclosed within ""
, it will print the description i.e Image(provider: SwiftUI.ImageProviderBox<SwiftUI.Image.(unknown context at $1d4ea7490).NamedImageProvider>)
.
So, Image("\(company.logo)")
is converted to Image("Image(provider: SwiftUI.ImageProviderBox<SwiftUI.Image.(unknown context at $1d4ea7490).NamedImageProvider>)")
and no asset of this name is found.
You can solve this in one of the 2 ways -
1 - Change the type of logo
to String
and set it to the name of the image.
let companyList = [
Company(..., logo: "BBC", ...),
...
]
2 - When creating CardView
, use company.logo
directly.
company.logo
.resizable()
.scaledToFit()
.padding()