Using Core Data in Swift to create an Entity

Posted By : Akshay Luthra | 14-Sep-2015

Swift is the latest technology that Apple intoduces for native iOS mobile app development. Core Data has been introduced in Swift for database management (Models). Though Swift has not restricted a developer to use Core Data only for managing the Models inside an app, but it is preferrable to use Core Data because of its light-weight interaction with the native components.

 

First, to create a mobile app project using Swift, click on Xcode -> go to File -> click on New -> click on Project. A pop-up will appear, click on Application under iOS and select Single View Application and click on Next.

 

Second, the project configuration screen will appear. Fill the fields that are required, select the language Swift, and check the "Use Core Data" option.

 

Your project has been set up and is ready to be used.

Third, click on the "xcdatamodelId" file from your project heirarchy. It will open a user interface for you to make Entities(tables) and Attributes(columns) of those entities. Click on the "Add Entity" option on the bottom-left corner to add an entity in your project.

 

Fourth, you can change the name of your Entity by double-clicking it. You can also add attributes and can give it the name you want and can set the type of the attributes.

 

Fifth, select the Entity that you have made, go to Editor and choose"Create NSManagedObject Subclass". Three pop-ups will appear before you one after the other. In the first two pop-ups, select the appropriate option and entity being displayed in front of you. In the third pop-up "Choose options for adding these files", check the option "Copy items if needed" and click on finish.

 

 

 

 

 

The Swift file with the class for your Entity has been created. You can use this class to work on the Entity that you made.

 

THANKS

 

About Author

Author Image
Akshay Luthra

Akshay Luthra has excellent experience in developing Cross Platform Mobile Apps using JavaScript and Titanium Framework. He loves listening to music and travelling new places.

Request for Proposal

Name is required

Comment is required

Sending message..