Skip to content

Repository files navigation

CoreData

Swift Core Data Basics with easy sample project.

Description

CoreData tutorial helps to create iPhone/iPad mobile application Project with CoreData.framework in Swift. I have created the sample project with clear comments and instructions.

Frameworks used

Table of Contents

  1. Necessary Environment.
  2. Shortcut Keys in Xcode.
  3. What is CoreData?
  4. Create Project with CoreData.
  5. Create Entities and Attributes in Model Layer.
  6. User Object life cycle : Create, Fetch, Update, Delete datas.
  7. Download Sample Project

1. Necessary Environment

  • MAC system with above OS X (Yosimite) version 10.10.*
  • Xcode version 7.*

2. Shortcut Keys in Xcode

  • ⌘ = Command
  • ⇧ = Shift
  • ⌥ = Option/Alt
  • ⌃ = Control
  • ⇠⇢ = Left/Right Arrow Keys
  • ⇡⇣ = Up/Down Arrow Keys

3. What is CoreData?

Core Data is a framework that you use to manage the model layer objects in your application. It provides generalized and automated solutions to common tasks associated with object life cycle and object graph management, including persistence.

Reference CoreData.framework

4. Create Project with CoreData

  1. Open Xcode.
  2. Create new project.(Fille > New > Project) - Shortcut Keys( ⇧ + ⌘ + N )
  3. Choose your template for you new project.
    • Select Single View Application
    • Select Language Swift
    • Select Use Core Data Check box.
  4. Save it in Workspace.

5. Create Entities and Attributes in Model Layer

  • Go to the Workspace.
  • Double Click ProjectName.xcodeproj and Open it in Xcode.
  • Open ProjectName.xcdatamodeld
  • Add Entite and Change name as you wish.
  • Add Attribute for your Entity and Select Data Type for Attributes.
  • Save it( ⌘ + S ).

6. Core Data Object life cycle

  • Create and Save object in local CoreData database.

Here's an example:

varUser=[NSManagedObject]()// Get AppDelegate Object letappDelegate=UIApplication.sharedApplication().delegateas!AppDelegate// Get Managed Context Object with help of AppDelegate.letmanagedContext=appDelegate.managedObjectContext// Create Entity Description with your entity name.letentity=NSEntityDescription.entityForName("YOUR_ENTITE_NAME",inManagedObjectContext:managedContext)// Create Managed Objec with help of Entity and Managed Context.letmanagedObject=NSManagedObject(entity: entity!,insertIntoManagedObjectContext: managedContext)// Set the value for your represent attributes.managedObject.setValue(VALUE,forKey: "YOUR_ATTRIBUTE")// Handle the Exception.do{trymanagedContext.save()// Save Mangaged ContextUser.append(managedObject)// Append your managedObject to the Database.}catchleterrorasNSError{print("Could not save \(error), \(error.userInfo)")}

7. Clone or Download Sample Project


Do you like it?

Do you like this repo? Share it on Twitter, Facebook, Google+ or anywhere you like so that more of us can use it and help. Thanks!

Created by Vignesh

alt text

About

Swift Core Data Basics with easy sample project.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages