Objective-c CoreData



#import "ViewController.h"正則表達式

#import "Person.h"//引入須要使用的實體數據庫

#import <CoreData/CoreData.h>測試



@interface ViewController ()fetch

{spa

    //管理對象上下文.net

    NSManagedObjectContext *_context;3d

}orm


@end 對象


@implementation ViewControllerip


- (void)viewDidLoad {

    [super viewDidLoad];

    

    _context = [self createDBContext];

    

    

//    [self addPersonWithName:@"測試員01" sex:@"" age:@18];

//    [self addPersonWithName:@"測試員02" sex:@"" age:@16];

//    [self addPersonWithName:@"測試員03" sex:@"" age:@28];

    

    NSLog(@"%@", [[self getPersonByName:@"測試員01"] valueForKey : @"sex"]);

//    [self removePerson:@"測試員02"];

    [self modifyPersonWithName:@"測試員03" sex:@"" age:@100];

    

    // Do any additional setup after loading the view, typically from a nib.

}

#pragma mark 1.建立管理上下文

- (NSManagedObjectContext *)createDBContext

{

    NSManagedObjectContext *context;

    //打開模型文件,參數爲nil則打開包中全部模型文件併合併成一個

    NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:nil];

    //建立解析器NSPersistentStoreCoordinator

    NSPersistentStoreCoordinator *storeCoordinator = [[NSPersistentStoreCoordinator alloc]initWithManagedObjectModel:model];

    //建立數據庫保存路徑(文件目錄)

    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];

    NSLog(@"%@", path);

    path = [path stringByAppendingPathComponent:@"YC.db"];

    

    //添加SQLite持久存儲到解析器

    NSError *error;

    [storeCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[NSURL fileURLWithPath:path] options:nil error:&error];

    if (error) {

        NSLog(@"數據庫打開失敗!錯誤: %@", error.localizedDescription);

    }

    else//數據庫建立成功(打開成功)

    {

        context = [[NSManagedObjectContext alloc]initWithConcurrencyType:NSMainQueueConcurrencyType];

        context.persistentStoreCoordinator = storeCoordinator;

        NSLog(@"數據庫打開成功!");

    }

    

    return context;

}


#pragma mark 2.插入數據

- (void)addPersonWithName : (NSString *)name sex : (NSString *)sex age : (NSNumber *)age

{

    //建立實體對象

    Person *person = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:_context];

    person.name = name;

    person.sex = sex;

    person.age = age;

    //保存上下文

    NSError *error;

    [_context save:&error];

    if (error) {

        NSLog(@"添加過程當中發送錯誤,錯誤: %@", error.localizedDescription);

    }

    else

    {

        NSLog(@"添加成功");

    }

}


#pragma mark 3.查詢數據

//NSPredicate(謂詞)是一個Foundation的類,它指定數據被獲取或者過濾的方式。它的語言就像SQLwhere和正則表達式的交叉同樣。提供了具備表現力的,天然語言界面來定義一個集合。(被搜索的邏輯條件)

//*zo*    z, zo, zoo, zooo....

//zo+    zo, zoo, zooo, zoooo...

- (NSArray *)getPersonByName : (NSString *)name

{

    //建立一個請求

    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Person"];

    request.predicate = [NSPredicate predicateWithFormat:@"%K=%@",@"name", name];

    //上下文執行請求,獲得查詢結果

    NSArray *array = [_context executeFetchRequest:request error:nil];

    return array;

}


#pragma mark 4.刪除數據

- (void)removePerson : (NSString *)name

{

    Person *person = [[self getPersonByName:name] firstObject];

    [_context deleteObject:person];

    NSError *error;

    [_context save:&error];

    if (error) {

        NSLog(@"刪除過程當中發生錯誤:%@", error.localizedDescription);

    }

    else

    {

        NSLog(@"刪除成功");

    }

}


#pragma mark 修改數據

//必須首先提取出對應的實體對象,而後經過修改對象屬性,最後保存

- (void)modifyPersonWithName : (NSString *)name sex : (NSString *)sex age : (NSNumber *)age{

    Person *person = [[self getPersonByName:name] firstObject];

    person.sex = sex;

    person.age = age;

    NSError *error;

    [_context save:&error];

    if (error) {

        NSLog(@"修改過程當中發生錯誤: %@", error.localizedDescription);

    }

    else

    {

        NSLog(@"修改爲功");

    }

}



- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end

相關文章
相關標籤/搜索