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