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

SOLVED: Project 11: Bookworm, app crash: UnsafeRawBufferPointer with negative count Error

Forums > 100 Days of SwiftUI

Good day!

I am currently on project 11 and faced the problem at How to combine Core Data and SwiftUI tutorial.

I do everything as on the video but my app keeps crashing, although Build succeeds. I even tried to launch final project provided with course materials and faced same probem: Xcode builds the app but everything crashes when it comes to simulator.

Also I noticed it happens when you try to save new book in the final Bookworm app

P.S. I checked this article https://www.hackingwithswift.com/forums/100-days-of-swiftui/day-54-app-crashes-when-adding-a-new-book/8608 , but it is does not help with my problem, so it must be something else

Thank you!

2      

Might be some coredata error , can you post what the error and where exactly ?

2      

Error is UnsafeRawBufferPointer with negative count (sorry, forgot to write in the request body)

It seems like app crashes when it tries to save data of new Students

2      

Your book work app file with @main method must be like below i assume

@main
struct BookwormApp: App {
    @StateObject private var dataController = DataController()

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

The AddView

struct AddBookView: View {
    @Environment(\.managedObjectContext) var moc
    @Environment(\.dismiss) var dismiss

    @State private var title = ""
    @State private var author = ""
    @State private var rating = 3
    @State private var genre = ""
    @State private var review = ""

    let genres = ["Fantasy", "Horror", "Kids", "Mystery", "Poetry", "Romance", "Thriller"]

    var body: some View {
        NavigationView {
            Form {
                Section {
                    TextField("Name of book", text: $title)
                    TextField("Author's name", text: $author)

                    Picker("Genre", selection: $genre) {
                        ForEach(genres, id: \.self) {
                            Text($0)
                        }
                    }
                }

                Section {
                    TextEditor(text: $review)
                    RatingView(rating: $rating)
                } header: {
                    Text("Write a review")
                }

                Section {
                    Button("Save") {
                        let newBook = Book(context: moc)
                        newBook.id = UUID()
                        newBook.title = title
                        newBook.author = author
                        newBook.rating = Int16(rating)
                        newBook.review = review
                        newBook.genre = genre
                        newBook.date = Date.now

                        try? moc.save()
                        dismiss()
                    }
                }
                .disabled(title.isEmpty || author.isEmpty || genre.isEmpty)
            }
            .navigationTitle("Add Book")
        }
    }
}

the datacontroller

class DataController: ObservableObject {
    let container = NSPersistentContainer(name: "Bookworm")

    init() {
        container.loadPersistentStores { description, error in
            if let error = error {
                print("Core Data failed to load: \(error.localizedDescription)")
            }
        }
    }
}

make sure date is of type date in coredata

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.