UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Day 54: App crashes when adding a new book

Forums > 100 Days of SwiftUI

I'm stuck on the first section of the Bookworm project.

I've updated the built-in data model, Persistence.swift, and BookwormApp.swift.

But every time I try to add a new book, the app crashes and I get the following error:

Thread 1: Fatal error: UnsafeRawBufferPointer with negative count

It seems the error is occuring in BookwormApp.swift where the @main property wrapper is.

However, any time I delete it, I get this error:

entry point (_main) undefined. for architecture x86_64

Here's what I have in BookwormApp.swift

import SwiftUI
@main
struct BookwormApp: App {
    let persistenceContainer = PersistenceController.shared

    var body: some Scene {
        WindowGroup {
            ContentView()
                .environment(\.managedObjectContext, persistenceContainer.container.viewContext)
        }
    }
}

And what I have in Persistence.swift

import CoreData

struct PersistenceController {
    static let shared = PersistenceController()

    let container: NSPersistentContainer

    init() {
        container = NSPersistentContainer(name: "bookworm")

        container.loadPersistentStores{ (storeDescription, error) in
            if let error = error as NSError? {
                fatalError("Unresolve Error: \(error)")
            }
        }
    }
}

And here's my project on Github.

What am I doing wrong?

Thanks in advance for your help!

3      

I think the problem is on this line

container = NSPersistentContainer(name: "bookworm")

try using a capital 'B' instead

container = NSPersistentContainer(name: "Bookworm")

That was the only real difference I could find between your code and mine and when I change mine to lowercase I get the same error you are talking about.

5      

Hi Natasha

The @main is the entry point for the app so you have to have that there. Some time error occurs next to it but does not usually mean that is the problom is there it just that where the compiler has entered. I looked at your code and @Fly0strich is correct that if you change "bookworm" to "Bookworm" because .xcdatamodelid is name Bookworm and it looking for that file and can not find it which is why you are getting the Fatel error.

6      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.