I'm just starting to learn Swift and I'm using the "Using Stacks to Create Views" learning module and I'm stuck in section 2 (Compose views) step 1 and the problem that I am having is that the code that is on the right hand section of the learning module is not the same code that I have in my Xcode contentview.swift in the ScrumdingerApp project. https://developer.apple.com/tutorials/app-dev-training/using-stacks-to-arrange-views
I'm kind of embarassed as I should be zipping right through this part..
The code in the lesson module is like this - import SwiftUI
struct ContentView: View { var body: some View { VStack { Image(systemName: "globe") .imageScale(.large) .foregroundColor(.accentColor) Text("Hello, world!") } .padding() } }
struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } }
and the code in my Scrumdinger contentview.swift is much longer and looks like this - this is what I downloaded>> // // ContentView.swift // Scrumdinger // // Created by Chet Shannon on 10/22/23. //
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 {
NavigationView {
List {
ForEach(items) { item in
NavigationLink {
Text("Item at \(item.timestamp!, formatter: itemFormatter)")
} label: {
Text(item.timestamp!, formatter: itemFormatter)
}
}
.onDelete(perform: deleteItems)
}
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
EditButton()
}
ToolbarItem {
Button(action: addItem) {
Label("Add Item", systemImage: "plus")
}
}
}
Text("Select an item")
}
}
private func addItem() {
withAnimation {
let newItem = Item(context: viewContext)
newItem.timestamp = Date()
do {
try viewContext.save()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
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 implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
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 }()
#Preview { ContentView().environment(.managedObjectContext, PersistenceController.preview.container.viewContext) }