WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

SOLVED: No exact matches in call to initializer

Forums > SwiftUI

Hello I am new with swiftui, my problem is that I can not add several names in the name part, I would not be able to know what problem I have because I get an error in "Text(service.name)" not being able to compile the code. what I am looking for is that in a variable of type array I can add several names and images instead of entering one by one. I wouldn't be able to pinpoint where the problem is, it would be a great help if you could help me, thanks.

struct Service: Identifiable  {
var id = UUID()
var name = [String]()
var logoImage: String

}

var services = [
Service(name: ["name1","name2","name3"], logoImage: "logo1"),
Service(name: ["name1","name2","name3"], logoImage: "logo2")
]

struct ServiceItem: View {
var service: Service

var body: some View {
    VStack {
        Image(service.logoImage)
            .resizable()
            .frame(width: 40, height: 40)
        Text(service.name)
            .font(.headline)
            .padding(.top,10)
    }
    .frame(width: 100, height: 140)
    .background(Color.white)
    .cornerRadius(10)
}

}

var body: some View {
    ScrollView(.horizontal) {
        HStack(spacing: 10) {
            ForEach(services) { service in
                ServiceItem(service: service)

            }
        }
    }
}

   

name is an Array of Strings and not a String. Text can't handle this. Either you choose one name like service.name[0], create a variable which gives back one String with all your names or loop through your array of names and create a Text view for each name.

2      

Luciano is asking a question that's covered in 100 Days of SwiftUI.

I cannot add several names in the name part, I do not know the problem because I get an error in "Text(service.name)" can't be compiled.

Hatshushira has the correct answer. Because services is an array, you need to extract just ONE of the items and access its String var. But that's not the REAL problem here.

Your code looks like you're trying to learn SwiftUI by guessing how to assemble Views. It looks like you're guessing how to declare data models (Services). It looks like you're guessing how to link models to views.

All of these concepts are answered in the 100 Days of SwiftUI program!

So, please answer these questions. Did you start at Day 0 ? What day of the program are you on?

The answer to your question is answered first in day 18, again in day 21, 24, etc. More information is added in days 30-34. Better ways to build data models are added in later days of the program.

@Hatsushira answered just a sliver of your question, but you'll soon run into other problems. And those problems won't be solvable from desparate forum posts.

Per favore, tell us what day you are on in the 100 Days of SwiftUI course.

1      

Hacking with Swift is sponsored by Emerge

SPONSORED Why are Swift reference types bad for app startup time, and what’s the performance cost of protocol conformances? That’s just a couple of the topics you can learn about on the Emerge blog — written by the app performance experts behind Emerge’s advanced app optimization and monitoring tools, based on their experience of working at companies like Apple, Airbnb, Snap, and Spotify.

Find out more

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.