-2

Currently I'm learning SwiftUI and there's a problem which I am facing. Compiler is unable to type check the expression.

It all started when I added Text(update.date). Also, Update is the structure which I have created where one of the data members is date of type String.

Is it related to the length of the code??

I have added the code and snapshot of the error.

import SwiftUI

struct UpdateList: View {
    
    var body: some View {
        
        NavigationView {
            List(updateData) { update in
                NavigationLink(destination: Text(update.text)) {
                    VStack(alignment: .leading) {

                        Text(update.title)
                            .font(.system(size: 20, weight: .bold))

                        Text(update.text)
                            .font(.subheadline)
                            .lineLimit(2)
                            .foregroundColor(Color(#colorLiteral(red: 0.2549019754, green: 0.2745098174, blue: 0.3019607961, alpha: 1)))

                        Text(update.date)   //all the errors occurred when typed this
                            .forgroundColor(.secondary)

                    }
                }
            }
            .navigationBarTitle(Text("Updates"))
        }
        
    }
}

struct UpdateList_Previews: PreviewProvider {
    static var previews: some View {
        UpdateList()
    }
}

struct Update: Identifiable{
    var id = UUID()
    var image: String
    var title: String
    var text: String
    var date: String
}

let updateData: [Update] = [
    Update(image: "Card1", title: "SwiftUI Advanced", text: "Take your app to App Store with advanced techniques like API data, packages and CMS", date: "JAN 1"),
    Update(image: "Card2", title: "WebFlow", text: "Design and animate a high converting landing page with advanced interations, payments and CMS", date: "OCT 17"),
    Update(image: "Card3", title: "ProtoPie", text: "Quickly Prototype advanced animations nd interactions for mobile and Web", date: "AUG 27"),
    Update(image: "Card4", title: "SwiftUI", text: "Learn how to code custom UI, animations and gestures and components in Xcode 11", date: "JUNE 26"),
    Update(image: "Card5", title: "Framer Playground", text: "Create powerful animations and interactions with Framer X code editor", date: "JUNE 11")
]

Any suggestions??

Here's the snapshot: https://i.stack.imgur.com/LXOrH.png

Thanks in advance

Hunter11
  • 11
  • 5
  • See this: https://www.hackingwithswift.com/quick-start/swiftui/how-to-format-dates-inside-text-views – aheze May 23 '21 at 20:30
  • Date formatters are of no use here. Have already tried that @aheze. – Hunter11 May 23 '21 at 20:47
  • 1
    Try and pull each view out into a computed property, that way the compiler error will be in that property and not the body. Here is an example https://stackoverflow.com/questions/67403140/drag-separators-in-swiftui/67452055#67452055. – Helperbug May 23 '21 at 21:54
  • Generally speaking, the error you're hitting actually means "Swift has no clue what's going on" and is not actually about types or type errors in all cases. As @patrick-wynne says, in this case, it is likely a simple typo. It seems like Swift doesn't know that `forgroundColor` isn't a method, because it isn't sure what type it isn't a method on. As @helperbug says, one way to debug this is to pull out each view into a computed property. More simply, I tend to delete views one-by-one until I find the one causing the problem, and then pull only that one out to figure out the problem. – deaton.dg May 23 '21 at 23:09
  • 1
    I extracted the contents of VStack into sub view and then it worked. Thanks for the help @Helperbug – Hunter11 May 24 '21 at 18:12

1 Answers1

1
.forgroundColor(.secondary)

should be

.foregroundColor(.secondary)
Patrick Wynne
  • 1,864
  • 15
  • 20
  • Thanks for mentioning it. Extracting the code into subview solved the problem. I referred this: https://www.youtube.com/watch?v=uC3X4FoielU – Hunter11 May 24 '21 at 18:14