Using Core Data with Xcode6

On today’s tutorial, I will show you how to set up a new Xcode project (via Xcode6 Beta-whatever-most-likely-4) and connect it to Core Data so you can store your information and get it back at will. I will also show you how to take over the world!

The Father of all evil…Anakin Skywalker, a.k.a Darth Vader.

 

…Oh wait, I was just informed that I can’t do that.

 

Apparently it's because these twinkie looking green things have already put a sinister plot into place.

Apparently it’s because these twinkie looking green things have already put a sinister plot into place.

 

Well then. I guess were just taking a Core Data tour (although I’d much rather be trolling around and setting up sinister bases).

 

Recipe Name: Space Station to Planet Core Data

Preparation Time: 5 minutes

Bake Time: 18.5 minutes

Cool Down & Icing: 9.4 minutes

Total Time: 33.4 minutes

 

Ingredients:

0. Xcode 6 (any version, the most recent version is beta 4)

1. Ray guns, galactic needle shooters, knick knacks and doo dads

2. I’ve just been informed since we can’t take over the world, number 1 is not needed

3. Oh well, it’s staying anyways I do what I want

4. Just been informed I can’t do what I want

5. Ray guns, galactic needle shooters, knick knacks and doo dads snacks

6. That’s it!

 

Preparation: You should already know what I’m about to say. Dig into that /Applications folder and launch Xcode! You’re going to make this a Single View Application, so select that option and click Next. Name the product “Core Data Space Station” (minus the parenthesis) and set your organizations name and identifier. Ensure “Swift” is selected from the drop-down menu (you can also use Objective-C – it’s purely your choice, but just know I’ll be using Swift so the syntax will be different). Make sure “Universal” is selected from the Devices drop-down, and lastly, make sure Core Data is checked. When all is said and done, your screen should look like this:

 

Time to rock-and-roll!

Time to rock-and-roll!

 

Save the new project somewhere (I saved mine to the desktop)  and let’s get ready to take over the I mean look at some code!

*cakenote: For this project, I will not be creating a Storyboard, but you can always go back later and connect the dots with your own custom storyboard at your own leisure.

Bake: The first thing I want you to do is look over the code in AppDelegate.swift and marvel at all the stuff you did not have to write. You may be thinking “Okay, so what, this is in EVERY new Xcode project”. And you are absolutely right! However, scroll down further…a bit further…yes, yes, my evil minion…I mean friend…further…and voila! Right underneath // Mark: – Core Data is all of the Core Data functionality…pre-wrote!

 

*cakenote: // Mark: – Core Data in Swift is the same thing as #pragma mark – Core Data in Objective-C

 

I won’t get into to much about it, but if you have any questions, pop open the Apple “Documentation and API Reference” (Window > Documentation and API Reference) manuals and see what you can dig up.

Let’s work in reverse order, shall we? Select the Core_Data_Space_Station.xcdatamodeld file, and you’ll be presented with the foundation of all Core Data, the Core Data Central Space Station, if you will (CDCSS?). What you’ll want to do here is click the “Add Entity+” button, and after it adds the new Entity, click (1 time) on the Entity name and rename it to something more grandiose, such as “EarthStation”. Yes, we’ll name it EarthStation for now.

 

Muahahah...You all look like ants from up here! >:)

Muahahah…You all look like ants from up here! >:)

 

Under the “Attributes” section, click the “+” button twice, naming the first row “earthlings” and the second row earthlingsThatAreProbablyAliens”. Set each Type to a String. When all is executed, your screen should now look just like this:

 

Phase II...

Phase II…

 

With that out of the way, we can finally move on with how to get and receive information from this “EarthStation”

Saving to Core Data is super easy. Pop open the “ViewController.swift” file, and near the top right underneath the import UIKit statement, write import CoreData. After that, directly underneath didReceiveMemoryWarning(), write this:

 

Read the notes for a better understanding of what's going on (The lines in green)

Read the notes for a better understanding of what’s going on (The lines in green)

 

Retrieving information from Core Data is just as easy. Underneath the method you just wrote, (saveToCoreData()), write this new snazzy method:

 

See the comments. Read the comments.

See the comments. Read the comments.

 

Lastly, we need a way to execute these functions when the view loads. Scroll back up to the viewDidLoad() method, and write these simple 2 lines of code:

 

Calling all functions...calling all functions...the last ship to Mars is about to take off!

Calling all functions…calling all functions…the last ship to Mars is about to take off!

 

Select a device to run this on (it won’t work with the black iOS device selection) and click the “Play” triangle. After everything is booted up and ready to roll, if you go back to Xcode (minimize the simulator) at the bottom of the screen in the debug menu you should see this:

 

Success!

Success!

 

 

Cool-Down and Icing: You have just seen a very fast, very easy way to use Core Data. However, this is just the tip of the Titanic Iceberg, there are books upon books (yes, full on 700 page books!) on how to use Core Data. It is a very useful tool if you plan on building applications that need a way to efficiently track information and save it, as well as get it back when the time is needed. I would recommend reading Apple’s Core Data documentation as a good starting point.

 

Eat it: Even though we weren’t able to take over the world as I had originally planned, things still worked out for the best. You got to see how in it’s most basic form, Core Data is used to store and retrieve objects. You also didn’t have to build a Storyboard to do it…but pray I do not alter the deal further*. Even though this tutorial is coming to a close, as always feel free to drop me suggestions, comments, and world domination concerns in the comments section of this recipe. Till next time, Happy Coding, and may the force be with you**!

 

References:

https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreData/cdProgrammingGuide.html    -Core Data infromation

* Star Wars – awesome

** Star Wars again – seriously, awesome. There was this one evil scene where Lord Vader was doing a force choke and…

 

“The sad truth is more evil is done by people who never make up their minds to be good or evil” – Hannah Arendt

3 thoughts on “Using Core Data with Xcode6

Leave a comment