iOS coredata old Code fallback reference

Source: Internet
Author: User
Tags uikit

The IPhone OS launched the 3.0 SDK in June 2009, one of which features the introduction of core data from the Mac SDK. is an ORM (Object relationships Mapping) solution, similar to Java's hibernate.

With this, you don't have to write cumbersome code like iOS simple SQLite, reduce the workload, or avoid using C's syntax to lower the technical threshold for iOS development. In short, can reduce development costs, improve project quality.

This article largely refers to Core Data Tutorial for IOS.

Create an iOS project

For convenience, create a project directly with the Xcode Tool Wizard, which automatically integrates support for core data.

Not necessarily create window-based projects, split view-based can also be used for ipad projects, or for iphone navigation-based projects.

Create entity

For example, Java instances need to write Java classes and corresponding mapping files (and possibly annotations). In iOS, two steps are required:

    • Graphical interface design tools, design entities, entity attributes, and entity relationships
    • The class that generates the entity, that is, the corresponding. h and. m files

Create a project such as AAA, then find Aaa.xcdatamodel:

Double-click it to bring up the Model editor. Create a person entity.

Click the plus sign to create a new entity, and then fill in the entity name in the text box of entity. Enter to see the following graphic names like UML class diagrams become person.

Then create the properties:

Click the plus sign in the Properties section, select Add Attribute, add an ID, number of:

Then, set the self-increment column:

Set the Name property again:

If there are multiple entities, continue to create, or create relationships between them.

The entity class is then generated to be called when the program is written.

To create a new file, note that in the Entity editor interface, you can select the person entity so that the generated file name is person:

Select Managed Object class:

And then:

Then select the desired entity, finish:

An entity class was generated in the project:

Using entities in your code

First, you need to include in the code header file that uses the person instance class:

#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
#import "Person.h"

Using the simple code of core data, create a person entity instance, save it, and traverse the data, equivalent to: SELECT * from Persons:

NSLog (@ ">>start app");
Person *person= (person *) [nsentitydescription insertnewobjectforentityforname:@ ' person ' inmanagedobjectcontext:[ Self Managedobjectcontext]];
[Email protected] "Zhang San";

Nserror *error;

if (![ [Self managedobjectcontext] save:&error]) {
NSLog (@ "error!");
}else {
NSLog (@ "Save person OK.");
}

Nsfetchrequest *request=[[nsfetchrequest alloc] init];
Nsentitydescription *entity=[nsentitydescription entityforname:@ "Person" inmanagedobjectcontext:[self Managedobjectcontext]];
[Request setentity:entity];

iOS coredata old code fallback reference

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.