Learn Courses My Dashboard

Errors which I don't know how to work with

I have been trying to do the Foundation Module 2 Wrap Up challenge and during that there shown up some errors which I don’t know how to work with, can somebody please help?
There is the code, the errors are on the three ForEach loops and they are the same (annot convert value of type ‘String’ to expected argument type ‘Binding’, Generic parameter ‘C’ could not be inferred (two errors on each ForEach loop))
import SwiftUI

struct QuoteDetail: View {
var quote:Quote
var body: some View {
ScrollView{
VStack(alignment: .leading){
Image(quote.Author)
.resizable()
.scaledToFill()
}
VStack(alignment: .leading){
Text(“Author:”)
.font(.headline)
.padding(.vertical)

            ForEach (quote.Author, id:\.self) { item in
                Text("• " + item)
            }
            Spacer()
            HStack{
                Text("Quote:")
                    .font(.headline)
                    .padding(.vertical)
                ForEach(quote.Quote, id: \.self){
                    i in Text(" " + i).font(.body)
                        .padding(.vertical)
                    
                }
                
            }
            Spacer()
            HStack{
                Text("Czech Translation:").font(.headline)
                    .padding(.vertical)
                ForEach(quote.CzechTranslation, id: \.self){
                    i in Text(" " + i).font(.body)
                        .padding(.vertical)
                }
            }
        }
    }
}

}

struct QuoteDetail_Previews: PreviewProvider {
static var previews: some View {
let model = QuoteModel()
QuoteDetail(quote: model.quotes[0])
}
}

@Tomaskrat17

Welcome to the community.

What does your Data Model look like and what does your json code look like?

Model: import Foundation

class Quote: Identifiable, Decodable {

var id:UUID?

var Author:String

var Quote:String

var CzechTranslation:String

var Image:String

}
Json:
[{
“Author”:“Lao Tzu”,
“Quote”: "Even the longes journey begins with the first step. ",
“Czechtranslation” : "I ta nejdelší cesta začíná prvním krokem. ",
“Image”:“Lao Tzu”

},{
“Author”:“Antoine de Saint-Exupéry”,
“Quote”: "Perfection is achieved, not when there is nothing to add, but when there is nothing left to take away. ",
“Czechtranslation” : "Dokonalosti nedosáhneme ve chvíli, kdy už není co přidat ale ve chvíli kdy už není co ubrat. ",
“Image”:“Antoine de Saint-Exupéry”

},{
“Author”:“Albert Einstein”,
“Quote”: “It’s not tham I’m so smart, it’s just that I stay with problems longer.”,
“Czechtranslation” : "Není to tím že jsem chytřejší, ale tím, že se problémy zabývám déle. ",
“Image”:“Albert Einstein”

},{
“Author”:“Michelangelo”,
“Quote”: "If people knew how hard I had to work to gain my mastery, it wouldn’t seem so wonderful at all. ",
“Czechtranslation” : "Kdyby lidé věděli, jak tvrdě musím pracovat abych dosáhl svého mistrovství, vůbec by jim to nepřipadalo tak úžasné. ",
“Image”:“Michelangelo”
}]

Data service:
import Foundation
class DataService {
static func getLocalData() → [Quote]{
let pathString = Bundle.main.path(forResource: “Quotes”, ofType: “json”)
guard pathString != nil else {
return Quote
}
let url = URL(fileURLWithPath: pathString!)
do{
let data = try Data(contentsOf: url)
let decoder = JSONDecoder()
do{
let quoteData = try decoder.decode([Quote].self, from: data)

            for r in quoteData {
                r.id = UUID()
                return quoteData
            }
        }
        catch{
            print(error)
        }
    }
    
    catch{
        print(error)
    }
    
    
 return[Quote]()
}

}

(all these are without errors, and they are basically copy writed from the video)

If you go back to the video of the challenge, Chris Ching shows that the initial view is a list of all the authors in a card style which has a background image and a quote overlaid on the image with the authors name at the bottom.

When you tap on that card you are presented with another screen that lists a series of quotes from that author.

From a data model perspective you have an author, an image and an array of quotes from that author.

So your data model ought to be something like this:

struct Quote: Decodable, Identifiable {
    var id: UUID?
    var author: String
    var image: String
    var quotes: [String]
}

So your initial screen would loop through each of the elements in the quotes array and display the image using the quote.image and then overlaid on that image, using a VStack, the first quote from the array of quotes and the authors name.

Does that make sense?