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

SOLVED: Compile Error: "Type 'TrackView' does not conform to protocol View"

Forums > macOS

I'm consistently running into this compile error telling me my TrackView does not conform to the View protocol. The code in my preview struct appears to be correct so I'm struggling to locate the origin of this error. Any guidance is helpful :)

import SwiftUI

struct TrackView: View {
    let track: Track
}

struct TrackView_Previews: PreviewProvider {
    static var previews: some View {
        TrackView(track: Track(trackId: 1, artistName: "Nirvana", trackName: "Smells Like Teen Spirit", previewUrl: URL(string: "abc")!, artworkUrl100: "https://bit.ly/teen-spirit"))
    }
}

3      

hi Justin,

you need to implement the body property that describes what you will see in the View. for example:

struct TrackView: View {
    let track: Track
    var body: some View {
      Text("This view probably shows you something about a Track.")
    }
}

hope that helps,

DMG

3      

@Justin missed a big lesson on protocols!

The code in my preview struct appears to be correct
so I'm struggling to locate the origin of this error.

Struct is a Box

Think of your struct as a box that holds your variables and methods for your specific purpose. This is easy to envision, you just add your vars and funcs to describe workouts, interesting places around town, store inventory, school students, or whatever you're designing.

However, sometimes you want to add some additional functionality to your struct so it works smoothly with other SwiftUI objects. For example, if you want to display your store inventory in a List(), then you'll want your struct to have a variable that makes one store item unique from all the others. This way the list knows which screwdriver your user tapped on.

To make your struct unique, you make it conform to the Identifiable protocol! Identifiable has one rule, your struct must implement a var named id. That's it! Now your structure is Identifiable.

Or maybe you have many workout structs and you want to sort your list of workout objects. How do you sort workouts? By muscle group? By difficulty? This is your problem to work out (ha! pun intended!). To allow SwiftUI to compare two workouts, the struct needs to implement the Comparable protocol. This protocol has ONE rule. Your struct must implement a function to compare two Workout objects and return true if the left hand object is "greater" than the right hand object.

Why doesn't Justin's View work?

You provide this code for your view:

struct TrackView: View {
    let track: Track
}

Here you are saying that you have a struct with one constant named track of type Track. But you are also saying your struct conforms to the View protocol. To conform to the View protocol your struct must implement a var named body! Furthermore, this var named body is a computed var that must generate some kind of View. This could be a Color, a Text(), a VStack(), a NavigationStack() or any one of the many types of views available via SwiftUI.

But the important part is: Your code is missing the var named body! This is required because you're declaring your struct conforms to the View protocol.

Keep coding!

3      

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!

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.