Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

xcode - Unmodified SwiftUI / Core Data default app code does not run in simulator?

When I create and run a new, unmodified, Core Data / SwiftUI app project in Xcode (12.3), the simulator shows a blank screen. Code below.

Upon creation of the new app project, SwiftUI code is generated that includes a List along with Add and Edit buttons. The UI displays correctly in Xcode's preview but not in the simulator.

This is the default code in the ContentView.swift file:


import SwiftUI
import CoreData

struct ContentView: View {
    @Environment(.managedObjectContext) private var viewContext

    @FetchRequest(
        sortDescriptors: [NSSortDescriptor(keyPath: Item.timestamp, ascending: true)],
        animation: .default)
    private var items: FetchedResults<Item>

    var body: some View {
        List {
            ForEach(items) { item in
                Text("Item at (item.timestamp!, formatter: itemFormatter)")
            }
            .onDelete(perform: deleteItems)
        }
        .toolbar {
            #if os(iOS)
            EditButton()
            #endif

            Button(action: addItem) {
                Label("Add Item", systemImage: "plus")
            }
        }
    }

    private func addItem() {
        withAnimation {
            let newItem = Item(context: viewContext)
            newItem.timestamp = Date()

            do {
                try viewContext.save()
            } catch {
                // Replace this...
                let nsError = error as NSError
                fatalError("Unresolved error (nsError), (nsError.userInfo)")
            }
        }
    }

    private func deleteItems(offsets: IndexSet) {
        withAnimation {
            offsets.map { items[$0] }.forEach(viewContext.delete)

            do {
                try viewContext.save()
            } catch {
                // Replace this...
                let nsError = error as NSError
                fatalError("Unresolved error (nsError), (nsError.userInfo)")
            }
        }
    }
}

private let itemFormatter: DateFormatter = {
    let formatter = DateFormatter()
    formatter.dateStyle = .short
    formatter.timeStyle = .medium
    return formatter
}()

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView().environment(.managedObjectContext, PersistenceController.preview.container.viewContext)
    }
}

You can reproduce this simply by creating a new iOS App project with SwiftUI and Core Data enabled:

xcode

Trying to wrap my head around this stuff. Getting stuck on the default code is not promising!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

IMHO Yes this seems like a bug with the example, might be worth submitting with a feedback

Problem:

  • Since the view is not inside the navigation view the navigation buttons are not visible
  • The tool bar modifier requires to use the ToolbarItem which is missing.

Overview:

  1. Please wrap the view contents inside a NavigationView
  2. Wrap the tool bar buttons inside ToolbarItem

Solution:

Replace the body computed property as follows:

    var body: some View {
        NavigationView {
            List {
                ForEach(items) { item in
                    Text("Item at (item.timestamp!, formatter: itemFormatter)")
                }
                .onDelete(perform: deleteItems)
            }
            .toolbar {
                
                #if os(iOS)
                ToolbarItem(placement: .navigationBarTrailing) {
                    
                    EditButton()
                }
                #endif
                
                ToolbarItem(placement: .navigationBarLeading) {

                    Button(action: addItem) {
                        Label("Add Item", systemImage: "plus")
                    }
                }
            }
        }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...