首页 > 学院 > 开发设计 > 正文

coreDate的使用

2019-11-09 18:49:26
字体:
来源:转载
供稿:网友

- (void)PRicticeCoreData

{

   

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

    NSString *storePath =  [document stringByAppendingPathComponent:@"lianxi.sqlite"];

    NSURL *storeUrl = [NSURL URLWithString:storePath];

    

   

    NSManagedObjectModel *mangerObjectModel = [[NSManagedObjectModel alloc]init];

    

   

    NSPersistentStoreCoordinator *persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc]initWithManagedObjectModel:mangerObjectModel];

    NSError *error = nil;

    [persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nilerror:&error];

    

   

    NSManagedObjectContext *context  = [[NSManagedObjectContext alloc]init];

    [context setPersistentStoreCoordinator:persistentStoreCoordinator];

    

   

    User *liwei = [[User alloc]initWithEntity:@"User"insertIntoManagedObjectContext:context];

    liwei.username = @"liwei";

    liwei.passWord = @"liwei";

    [context save:&error];

    

   

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

    NSArray *resultArray = [context executeFetchRequest:requesterror:nil];

    [self.dataArray setArray:resultArray];

    

 

}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表