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

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)

            }
        }
    }
}

1      

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.

3      

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.

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.