Getting scope errors in Xcode 15, beta 8

When I am converting a VisionOS project to use persistence with SwiftData, I get 2 errors. The 2 errors are the same, they are Cannot find 'item' in scope. No, I am not stupid. All other lines in the code can find it, so why not these two lines? Can you please help? Here is the code:

//
//  TodoListView.swift
//  Vision To-Do
//
//  Created by Nicolas Tama on 9/3/23.
//

import SwiftUI
import SwiftData

struct TodoListView: View {
    @Environment(\.modelContext) private var modelContext
    @State var list: TodoList
    
    @State private var showAddTodoAlert: Bool = false
    @State private var newTodoTitle: String = ""
    
    var body: some View {
        List {
            Section("Todo") {
                ForEach(list.items.filter { $0.isDone == false }){ item in
                    HStack {
                        Button {
                            item.isDone.toggle()
                        }
                    }
                } label: {
                    Image(systemName: item.isDone ? "circle.fill" : "circle") //Here is the error
                }
                Text(item.title) //Here is the error as well
                Spacer()
            }
            
            Section("Done") {
                ForEach(list.items.filter { $0.isDone }){ item in
                    HStack {
                        Button {
                            item.isDone.toggle()
                        }
                    } label: {
                        Image(systemName: item.isDone ? "circle.fill" : "circle")
                    }
                    Text(item.title)
                    Spacer()
                }
            }
        }
        
        .navigationTitle("Details for \(list.title)")
        .toolbar {
            Button("Add Todo") {
                showAddTodoAlert.toggle()
            }
        }
        .alert("Add Todo", isPresented: $showAddTodoAlert) {
            TextField("Todo Title", text: $newTodoTitle)
            Button("Cancel", role: .cancel, action: {})
            Button("Create") {
                let todo = TodoItem(title: newTodoTitle)
                modelContext.insert(todo)
                list.items.append(todo)
            }
        }
        .id(list.id)
    }
}

No, I am not stupid.

No, but you missed something. It has nothing to do with Xcode 15…

I copy code to add line numbers

1. import SwiftUI
2. import SwiftData
3. 
4. struct TodoListView: View {
5.     @Environment(\.modelContext) private var modelContext
6.     @State var list: TodoList
7.     
8.     @State private var showAddTodoAlert: Bool = false
9.     @State private var newTodoTitle: String = ""
10.     
11.     var body: some View {
12.         List {
13.             Section("Todo") {
14.                 ForEach(list.items.filter { $0.isDone == false }){ item in
15.                     HStack {
16.                         Button {
17.                             item.isDone.toggle()
18.                         }
19.                     }
20.                 } label: {
21.                     Image(systemName: item.isDone ? "circle.fill" : "circle") //Here is the error
22.                 }
23.                 Text(item.title) //Here is the error as well
24.                 Spacer()
25.             }
26.             
27.             Section("Done") {
28.                 ForEach(list.items.filter { $0.isDone }){ item in
29.                     HStack {
30.                         Button {
31.                             item.isDone.toggle()
32.                         }
33.                     } label: {
34.                         Image(systemName: item.isDone ? "circle.fill" : "circle")
35.                     }
36.                     Text(item.title)
37.                     Spacer()
38.                 }
39.             }
40.         }
41.         
42.         .navigationTitle("Details for \(list.title)")
43.         .toolbar {
44.             Button("Add Todo") {
45.                 showAddTodoAlert.toggle()
46.             }
47.         }
48.         .alert("Add Todo", isPresented: $showAddTodoAlert) {
49.             TextField("Todo Title", text: $newTodoTitle)
50.             Button("Cancel", role: .cancel, action: {})
51.             Button("Create") {
52.                 let todo = TodoItem(title: newTodoTitle)
53.                 modelContext.insert(todo)
54.                 list.items.append(todo)
55.             }
56.         }
57.         .id(list.id)
58.     }
59. }

You have no error on lines 34 and 36: look carefully; that's because they are in closure

item is defined in the closure of forEach at line 14. It does not exist outside.

Line 21 and 23 are outside, hence item unknown there.

In addition,

  • on what does label at line 20 would have applied ?
  • which item would be used on line 23 ?

SOLUTION:

  • Remove closing curly brace on line 19
  • insert it on line 25
Getting scope errors in Xcode 15, beta 8
 
 
Q