CoreData has never been used before. I tried it recently. It saves a lot of code and is very convenient. Of course, it will only be used.
Create a project first
By default, the system will add some things to you. As for how these things are made, you can search for them on Google. As you said, I will only use them. There is a. xcdatamodeld file under the project directory, which is the CZ extension? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> keys/J0tTU2tXiwO/keys + keys/keys + 3cSj0M3Sss/gtbht2t1_v2 + 3b/keys =" http://www.2cto.com/uploadfile/Collfiles/20140306/2014030614011142.jpg "alt =" \ ">
Now we create a Modeld named Dog. At the same time, the first letter of the name should be capitalized. Next we will configure some properties of this Dog. Click "+" to add a property. You can select the Type of the property.
Click CreateNSManagedObject SubClass... in the Editor and select Dog. Then, Create will have a Dog class.
Of course, there is no need to care about these things.
This is the use of this thing now. Well, I admit this one is not very descriptive, because my database is not very good at learning. When a project is created, it automatically describes an attribute in AppDelegate.
We use NSManagedObjectContext to add, delete, modify, and Query files. First, insert a piece of data. Do not forget to import the Dog header file, although this is common sense.
Create a Dog object. (@ (1) = [NSNumbernumberWithInt: 1];)
Dog * dog = [NSEntityDescription insertNewObjectForEntityForName: @ "Dog" inManagedObjectContext: self. managedObjectContext]; [dog setName: @ "Huahua"]; [dog setAge: @ (1)]; [dog setSex: @ (0)];
Save
NSError * error = nil; BOOL isSave = [self. managedObjectContext save: & error]; if (! IsSave) {NSLog (@ "error: % @, % @", error, [error userInfo]);} else {NSLog (@ "saved successfully ");}
Then, you can query the data in the table and describe a variable array to save them.
@property (strong, nonatomic) NSMutableArray *dataArray;
// Create a data retrieval request NSFetchRequest * request = [[NSFetchRequest alloc] init]; // you can specify the object type NSEntityDescription * entity = [NSEntityDescription entityForName: @ "Dog" inManagedObjectContext: self. managedObjectContext]; // sets the request entity [request setEntity: entity]; // specifies the way NSSortDescriptor * sortDescriptor = [[NSSortDescriptor alloc] initWithKey: @ "name" ascending: NO]; NSArray * sortDescriptions = [[NSArray alloc] containers: sortDescriptor, nil]; [request setSortDescriptors: sortDescriptions]; NSError * error = nil; // execute the data retrieval request, returns the array NSMutableArray * mutableFetchResult = [[self. managedObjectContext executeFetchRequest: request error: & error] mutableCopy]; if (mutableFetchResult = nil) {NSLog (@ "Error: % @, % @", error, [error userInfo]);} self. dataArray = mutableFetchResult; for (Dog * dog in self. dataArray) {NSLog (@ "age: % @ --- sex: % @ --- name: % @", dog. age, dog. sex, dog. name );}
Now let's change the name of an object.
[Dog setName: @ "asthma dog"]; NSError * error; BOOL isUpdateSuccess = [self. managedObjectContext save: & error]; if (! IsUpdateSuccess) {NSLog (@ "error: % @, % @", error, [error userInfo]);} else {NSLog (@ "Update successful! ");}
Delete the data.
[Self. managedObjectContext deleteObject: dog]; [self. dataArray removeObject: dog]; NSError * error; if (! [Self. managedObjectContext save: & error]) {NSLog (@ "Error: % @, % @", error, [error userInfo]);} else {NSLog (@ "deleted successfully! ");}
Now I will be so much and hope to help you. In addition, the system saves data to Documents by default. You can modify it in this method of AppDelegate. Of course, this is not important.