Whiffer / SwiftUI-Core-Data-Test

Sample program to demonstrate how CoreData can be used with SwiftUI.
153 stars 19 forks source link

Date and Bool attributes #8

Closed psides83 closed 4 years ago

psides83 commented 4 years ago

I'm working with multiple Dates and Bools in my data model. I've been able to implement them nicely except for one place.

Here in the core data file with the Int values of this loop (I think this is a loop);

// MARK: - Database setup

public class func initialDbSetup() -> Void {

    if Item.count() == 0 {
        for i in 0...4 {
            let item = Item.createItem(name: "Item \(i)", order: i)
            for j in 0...4 {
                _ = Attribute.createAttributeFor(item: item, name: "Attribute \(i).\(j)", order: j)
            }
        }
    }
}

I have declared the Bools like this.

someBool: (true)

Xcode accepts it. Although I'm not sure it's correct.

As for Date, the only way I have been able to declare it is like so.

someDate: (Date()

Xcode accepts this but it crashes the app at runtime.

With both of these the i and j are missing so I'm not sure what that will effect. I'm honestly not sure what this func is doing for the model.

I apologize for my ignorance but I'm just learning programming from scratch in the last month. Can you provide any help with this?

Whiffer commented 4 years ago

The only thing my initialDbSetup() function does is populate the database with some sample data so I don’t have to do it manually via the UI. If it’s OK for your database to be empty at startup then you do not need it. But if you do need or want initial data, and you have modified the Core-data data model, then you must also modify that function to create items that are appropriate for your data model.

One thing to keep in mind when making changes to your core data data model during development is to delete any existing data before trying to run the app. The easiest way to do this is to delete the app from your device or the simulator before you run the app.

If you need more help getting up to speed with Core Data, Swift, or iOS programming in general, there are many great beginner tutorials available online. I also find that stackoverflow.com is a very good place to ask questions or even find answers to questions that other beginners have already asked.

On Mar 14, 2020, at 6:04 AM, psides83 notifications@github.com wrote:

I'm working with multiple Dates and Bools in my data model. I've been able to implement them nicely except for one place.

Here in the core data file with the Int values of this loop (I think this is a loop);

// MARK: - Database setup

public class func initialDbSetup() -> Void {

if Item.count() == 0 {
    for i in 0...4 {
        let item = Item.createItem(name: "Item \(i)", order: i)
        for j in 0...4 {
            _ = Attribute.createAttributeFor(item: item, name: "Attribute \(i).\(j)", order: j)
        }
    }
}

} I have declared the Bools like this.

someBool: (true)

Xcode accepts it. Although I'm not sure it's correct.

As for Date, the only way I have been able to declare it is like so.

someDate: (Date()

Xcode accepts this but it crashes the app at runtime.

With both of these the i and j are missing so I'm not sure what that will effect. I'm honestly not sure what this func is doing for the model.

I apologize for my ignorance but I'm just learning programming from scratch in the last month. Can you provide any help with this?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/Whiffer/SwiftUI-Core-Data-Test/issues/8, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABE2LJ3BSR35APT23ZJRKKDRHN6HLANCNFSM4LJBCXTQ.

psides83 commented 4 years ago

Thanks for the meaningful reply. Huge help! And very good news to me that this is only providing the sample data. I’ll delete the function all together.