SwiftUI Landmark tutorial - keypath not compiling

I'm going through the SwiftUI tutorial: https://developer.apple.com/tutorials/swiftui/building-lists-and-navigation.

The following example code doesn't compile:

List(landmarkData, id: \.id) { landmark in
    ...
}

The error points to the \.id and says: Type '_' has no member 'id'. The landmarkData is [Landmark] and Landmark does have a property id: Int.

I tried:

List(landmarkData, id: \Landmark.id) ...

And get a different error: Cannot convert value of type 'WritableKeyPath<Landmark, Int>' to expected argument type 'KeyPath<_, _>'

Any ideas? This is Xcode 11, beta 6.

Rob

I just downloaded this tutorial and built it using Xcode 11, beta 6 without any problems. Try to "Clean Build Folder" before building and retry.

1 Like

The downloaded code doesn't use the keypath. They show it in the tutorial, but change it in a later step.

That said, I did just get it to compile. I had an empty view inside the List { } block. I'd say it's a buggy error message.

Are you asking about step 1 of section 5? I believe it's intentional that that step doesn't compile; that's why there's no preview associated with it. And the next step says "Complete the list by…".

I agree that the error message is opaque.

Yes, that's the step. I thought I was getting an error even when I had something in the block, after the second step, but maybe not. This beta of Xcode is still doing some weird things.

@robnik Did you manage to fix this?

I've ran into the exact same issue but I am having no luck correcting it.

I think I was only getting that error when I had another error within the closure block below it. The problem is that a bug in the compiler causes it to report the error there, at the top of the block, even if the real error is 50 lines later. So both the error location and message are completely misleading. This and some other issues with runtime layout bugs, and lack of documentation, led me to abandon SwiftUI for now.

Terms of Service

Privacy Policy

Cookie Policy