Building Lists & Navigation: Extra argument 'id' in call

I was going this tutorial:


https://developer.apple.com/tutorials/swiftui/building-lists-and-navigation


I got to the Generating Previews section:


https://developer.apple.com/tutorials/swiftui/building-lists-and-navigation#generating-previews-dynamically


It appears the code in Step 3 is broken. In particular Line 18 of LandmarkList.swift.


ForEach(["iPhone SE", "iPhone XS Max"], id: \.self) { deviceName in LandmarkList()


XCode complains about id: \.self


"Extra argument 'id' in call"


Does anyone know why this is broken in their completed code? Thanks in advance.

Answered by Claude31 in 375594022

So just try replacing by


       ForEach(["iPhone SE", "iPhone XS Max"], content: \.self) { deviceName in



But the safest would probably to upgrade to beta 5 (things are moving a lot in SwiftUI).

Did you copy the exact code:



struct LandmarkList: View {
    var body: some View {
        NavigationView {
            List(landmarkData) { landmark in
                NavigationLink(destination: LandmarkDetail(landmark: landmark)) {
                    LandmarkRow(landmark: landmark)
                }
            }
            .navigationBarTitle(Text("Landmarks"))
        }
    }
}



struct LandmarkList_Previews: PreviewProvider {
    static var previews: some View {
        ForEach(["iPhone SE", "iPhone XS Max"], id: \.self) { deviceName in
            LandmarkList()
                .previewDevice(PreviewDevice(rawValue: deviceName))
        }
    }



If yes, did you try removing the id argument ?

SwiftUI is evolving between each version of XCode and doc may not be updated.

Does autocompletion propose you some signature for ForEach ?

  1. Yes, that's the code I have.
  2. If I remove the id, I get this error message.
    1. Cannot convert value of type '(String) -> _ModifiedContent<LandmarkList, _TraitWritingModifier<PreviewDevice?>>' to expected argument type '(_) -> _'
  3. ForEach autocompletes to this
    1. ForEach(<#T##data: _##_#>, content: <#T##(_.Element.IdentifiedValue) -> _#>)
  4. I am using XCode Version 11.0 beta 3 (11M362v)
Accepted Answer

So just try replacing by


       ForEach(["iPhone SE", "iPhone XS Max"], content: \.self) { deviceName in



But the safest would probably to upgrade to beta 5 (things are moving a lot in SwiftUI).

Upgrade to Beta 5 works. Leave it as id. Do not change to content.


Thanks Claude31.

The below line worked. Using .identified(by: ) helped me to get rid of the error


ForEach([“iPhone SE”, “iPhone XS Max”].identified(by: \.self)) { deviceName in
Building Lists & Navigation: Extra argument 'id' in call
 
 
Q