![]() ![]() When this process completes, two new files are created: Commit+CoreDataClass.swift and Commit+CoreDataProperties.swift. Make sure the Commit entity is checked then click Next again.įinally, look next to Group and make sure you see a yellow folder next to "Project 38" rather than a blue project icon, and click Create. Make sure your data model is selected then click Next. Still inside the Core Data editor, go to the Editor menu and choose Create NSManagedObject Subclass. The second way is to create our own custom NSManagedObject subclass, which right now is the preferred way forward because it lets us take the dynamically generated class and customize it. You’ve seen codegen, which is the first way Xcode can help us create objects in Core Data. You’ll get compiler errors now because the Commit class no longer exists, but that’s OK. So, let’s put the codegen feature to one side for now – go back to the Core Data editor, change Codegen back to “Manual/None”, then press Cmd+S to save and Cmd+B to rebuild your app. Sometimes that’s OK, but usually it’s not. Yes, even though we marked all the attributes as non-optional in the Core Data editor, that just means they need to have values by the time they get saved – Xcode will quite happily let them be nil at other times. ![]() Xcode’s auto-generated class has one small annoyance, and you’ll see it if you try using code completion to view its properties: all four of the properties it made for us are optional, so name is a String?, date is an Date?, and so on. That means “set the date property to the current date.” First, try adding this line below the previous three: commit.date = Date() However, this feature is imperfect, at least right now – although that might change at any point in the future as Apple updates Xcode. You get access to its attributes as properties that you can read and write, and any changes you make will get written back to the database when you call our saveContext() method. You can’t see it in the project – it’s dynamically generated when the Swift code is being built – but it’s there for you to use, as you just saw. Open ViewController.swift and add this code at the end of viewDidLoad(): let commit = Commit()Ĭan you figure out what Xcode has done for us? The Codegen value is short for “code generation” – when you pressed Cmd+B to build your project, Xcode converted the Commit Core Data entity into a Commit Swift class. What just changed might look small, but it’s remarkably smart. ![]() Change it to “Class Definition”, press Cmd+S to save the change, then press Cmd+B to have Xcode build the project. Let’s look at it briefly now: open Project38.xcdatamodeld, select the Commit entity again, then look in the data model inspector for the “Codegen” option. There are two ways Xcode can help, one of which isn’t good enough for this project but is slowly getting better – maybe when I update this project next it will be update to scratch. So, it's time for step three: creating objects in Core Data so that we can fetch and store data from GitHub. It's not perfect, as you'll see shortly, but it's certainly a head start. If this sounds complicated, relax: Xcode can do quite a bit of work for us. For example, you already saw the hasChanges property of a managed object context – that automatically gets set to true when you make changes to your objects, because Core Data tracks when you change properties that are marked the scenes, effectively means "extra code will automatically be provided when the program runs." It's a bit like functionality injection: when you say "this property is then Core Data will add getters and setters to it when the app runs so that it handles things like change tracking. This is a Core Data subclass of NSObject that uses a unique keyword, to provide lots of functionality for its properties. After changing those objects, we can save them back to the persistent store, which is when Core Data converts them back from objects to database records.Īll this is done using a special data type called NSManagedObject. In our app, Core Data is responsible for reading data from a persistent store (the SQLite database) and making it available for us to use as objects. Creating an NSManagedObject subclass with Xcode ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |