Skip to content

[SR-15850] Misleading/Unhelpful Compiler Diagnostic when SwiftUI List Selection Property is Non-Optional #58124

Open
@swift-ci

Description

@swift-ci
Previous ID SR-15850
Radar None
Original Reporter bdkjones (JIRA User)
Type Improvement

Attachment: Download

Additional Detail from JIRA
Votes 0
Component/s swift
Labels Improvement
Assignee None
Priority Medium

md5: 6eb07fd76661a6b59e6f748a451eff14

Issue Description:

Consider this simple SwiftUI code, where we're populating a List with different "categories" the user can select:

enum SettingsCategory: CaseIterable, Identifiable
{
    case one
    case two

    var id: UUID {
        return UUID()
    }
}


struct CustomView: View
{
    @State private var selectedCategory: SettingsCategory = .one

    var body: some View
    {
        List(SettingsCategory.allCases, id: \.self, selection: $selectedCategory) { category in
        {
             ...
        }
    }
}

Xcode 13.2 (13C90) Spits out an error on the List line that's extremely unhelpful:

Generic parameter 'SelectionValue' could not be inferred
Explicitly specify the generic arguments to fix this issue
No Exact matches in call to initializer

Xcode also offers an autofix, but if you use it you're now REALLY lost, because here's what it does to that "list" line:

List<<#SelectionValue: Hashable#>, ForEach<[SettingsCategory], SettingsCategory, some View>>(SettingsCategory.allCases, id: \.self, selection: $selectedCategory) { category in

The ACTUAL problem is very simple: the selectedCategory property needs to be an optional.

It would be VERY useful if the compiler diagnostic would point that out, because it's easy to forget to make the selection property optional when you're typing fast and flying along. As such, this seems like a fairly common, routine error that people will make. The existing compiler diagnostic, while technically true, is far from helpful in resolving the problem.

(I've filed the issue here because I gather this is the right spot for improving diagnostic messages.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions