"Generic parameter 'Label' could not be inferred" on Toggle during SwiftUI tutorial "Handling User Input"

Hi developers,


As I was following the SwiftUI tutorial "Handling User Input" on creating a toggle and bind the data to the environment object "userData", I got an error named "Generic parameter 'Label' could not be inferred", and here is the corresponding code where this error popped up:


Toggle(isOn: $userData.showFavoritesOnly) {
                    Text("Show Favorites Only")
}

and here is the whole file of the code, with the above line of code styled as bold:


/*
See LICENSE folder for this sample’s licensing information.

Abstract:
A view showing a list of landmarks.
*/

import SwiftUI

struct LandmarkList: View {

    @EnvironmentObject private var userData: UserData

    var body: some View {
        NavigationView {
            List {
                Toggle(isOn: $userData.showFavoritesOnly) {
                    Text("Show Favorites Only")
                }
                ForEach(userData.landmarks) { landmark in
                    if !self.$userData.showFavoritesOnly || landmark.isFavorite {
                        NavigationLink(destination:LandmarkDetail(landmark: landmark).environmentObject(self.userData)
                        ) {
                            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))
                .previewDisplayName(deviceName)
        }.environmentObject(UserData())
    }
}

I copied and pasted the completed version of the code and it was built successfully, but I didn't find any sytactic difference between my code as shown above and the completed version, and here is the completed version:


/*
See LICENSE folder for this sample’s licensing information.

Abstract:
A view showing a list of landmarks.
*/

import SwiftUI

struct LandmarkList: View {

    @EnvironmentObject var userData: UserData

    var body: some View {
        NavigationView {
            List {
                Toggle(isOn: $userData.showFavoritesOnly) {
                    Text("Show Favorites Only")
                }
                ForEach(userData.landmarks) { landmark in
                    if !self.$userData.showFavoritesOnly || landmark.isFavorite {
                        NavigationLink(destination:LandmarkDetail(landmark: landmark).environmentObject(self.userData)
                        ) {
                            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))
                .previewDisplayName(deviceName)
        }.environmentObject(UserData())
    }
}

I tried to fix that by adding a <Text> after Toggle, but even though the problem was fixed, a new error popped up as "Unable to infer complex closure return type; add explicit type to disambiguate" on the ForEach(....) line


I wonder how I can solve this problem?


Thanks!

In your code, userData is private, so no more accessible outside.

Try remove the private keyword


    @EnvironmentObject private var userData: UserData

I tried but it still didn’t work

Accepted Answer

Hi! I got the same weird error. You can solve it by changing the line:


if !self.$userData.showFavoritesOnly || landmark.isFavorite {


to:


if !self.userData.showFavoritesOnly || landmark.isFavorite {

Thank you, this helped me!

It works! Thank you!

"Generic parameter 'Label' could not be inferred" on Toggle during SwiftUI tutorial "Handling User Input"
 
 
Q