coreData使用

简介

  Core Data是iOS5之后才出现的一个框架,它提供了对象-关系映射(ORM)的功能,即能够将OC对象转化成数据,保存在SQLite数据库文件中,也能够将保存在数据库中的数据还原成OC对象。

1.新建工程,记得勾选Use Core Data

2.建立好以后可以看到xxx.xcdatamodeld,在这里可以添加实体和实体的属性。需要注意的是:实体名字必须以大写开头

3.然后新建一个file,记得是NSManagedObject cubclass

4.勾选自己建立的工程

5、勾选建立的实体

6.我们就可以看到建立好的实体是有4个文件

7.

#import "ViewController.h"

#import "Dog.h"

#import "AppDelegate.h"

#import <CoreData/CoreData.h>

 

@interface ViewController ()

{

    AppDelegate *app;

}

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    app = [UIApplication sharedApplication].delegate;

    [self createButtons];

}

 

//创建按钮 

- (void)createButtons{

    NSArray *array = @[@"", @"", @"", @""];

    for (int i = 0; i < 4; i++) {

        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

        button.frame = CGRectMake(30 + i * 60, 100, 40, 40);

        [button setTitle:array[i] forState:UIControlStateNormal];

        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

        [button addTarget:self action:@selector(buttonDidClicked:) forControlEvents:UIControlEventTouchUpInside];

        button.tag = i;

        [self.view addSubview:button];

        

    }

}

 

- (void)buttonDidClicked:(UIButton *)button{

    switch (button.tag) {

        case 0:

            //add

            [self coreDateAdd];

            break;

        case 1:

            //delete

            [self coreDataDelete];

            break;

        case 2:

            //update

            [self coreDataUpdate];

            break;

        case 3:

            //search

            [self coreDataSearch];

            break;

        default:

            break;

    }

}

 

//增

- (void)coreDateAdd{

    Dog *dog = [NSEntityDescription insertNewObjectForEntityForName:@"Dog" inManagedObjectContext:app.managedObjectContext];

    dog.name = [NSString stringWithFormat:@"汪汪%d", arc4random()%10];

    dog.sex = @"";

    dog.age = [NSString stringWithFormat:@"%d", arc4random()%15];

    [app.managedObjectContext save:nil];

    NSLog(@"add success");

}

 

//删 

- (void)coreDataDelete{

    //读取类

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Dog" inManagedObjectContext:app.managedObjectContext];

    //建立请求

    NSFetchRequest *request = [[NSFetchRequest alloc] init];

    [request setEntity:entity];

    

    //设置检索条件

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name=%@", @"汪汪7"];

    [request setPredicate:predicate];

    

    NSArray *array = [app.managedObjectContext executeFetchRequest:request error:nil];

    if (array.count) {

        for (Dog *dog in array) {

            [app.managedObjectContext deleteObject:dog];

        }

        [app.managedObjectContext save:nil];

        NSLog(@"delete succeed");

    } else{

        NSLog(@"no data");

    }

}

 

//改

- (void)coreDataUpdate{

    //读取类

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Dog" inManagedObjectContext:app.managedObjectContext];

    //建立请求

    NSFetchRequest *request = [[NSFetchRequest alloc] init];

    [request setEntity:entity];

    

    //设置检索条件

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name!=%@", @"汪汪23"];

    [request setPredicate:predicate];

    

    //遍历

    NSArray *array = [app.managedObjectContext executeFetchRequest:request error:nil];

    if (array.count) {

        for (Dog *dog in array) {

            dog.name = @"汪汪23";

        }

        [app.managedObjectContext save:nil];

        NSLog(@"update succeed");

    } else{

        NSLog(@"no result");

    }

    

}

 

//查

- (void)coreDataSearch{

    //读取类

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Dog" inManagedObjectContext:app.managedObjectContext];

    //建立请求

    NSFetchRequest *request = [[NSFetchRequest alloc] init];

    //建立请求的是哪一类

    [request setEntity:entity];

    //遍历数组

    NSArray *array = [app.managedObjectContext executeFetchRequest:request error:nil];

    for (Dog *dog in array) {

        NSLog(@"%@", dog.name);

    }

    

}

 

 

 

 

@end

posted @ 2016-05-09 15:58  南城半夏北风过丶  阅读(151)  评论(0编辑  收藏  举报