zoukankan      html  css  js  c++  java
  • CoreData的用法

    #import "ViewController.h"
    #import "Student.h"
    
    @interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
    {
        NSArray *_dataArray;
        //负责应用与数据库交互
        NSManagedObjectContext *_context;
        
        NSIndexPath *_indexPath;
    }
    @property (weak, nonatomic) IBOutlet UITextField *nameTextField;
    @property (weak, nonatomic) IBOutlet UITextField *ageTextField;
    @property (weak, nonatomic) IBOutlet UITableView *tableView;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        [self initManagerObjectContext];
    }
    -(void)initManagerObjectContext
    {
        //1.conredata文件的路径
        NSString *path = [[NSBundle mainBundle] pathForResource:@"CoreData.momd" ofType:nil];
        //文件路径
        NSURL *fileUrl = [NSURL fileURLWithPath:path];
        //2.加载本地所有模型
        NSManagedObjectModel *model = [[NSManagedObjectModel alloc] initWithContentsOfURL:fileUrl];
        //3.创建数据库存储协议器(sqlit数据存储方式)
        NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
        NSString *storePath = [NSHomeDirectory() stringByAppendingString:@"/Documents/student.sqlite"];
        NSLog(@"%@",NSHomeDirectory());
        NSURL *storeUrl = [NSURL fileURLWithPath:storePath];
        NSError *error;
        NSPersistentStore *store = [psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error];
        if (!store) {
            NSLog(@"%@",error.localizedDescription);
        }
        //创建数据库管理对象
        _context = [[NSManagedObjectContext alloc] init];
        _context.persistentStoreCoordinator = psc;
    }
    - (IBAction)addToSqlite:(id)sender {
        Student *model = [NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:_context];
        model.name = _nameTextField.text;
        model.age = @(_ageTextField.text.integerValue);
        NSError *error;
        if (![_context save:&error]) {
            NSLog(@"%@",error.debugDescription);
        }
    }
    - (IBAction)selectFromSqlite:(id)sender {
        NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Student"];
        //request.predicate = [NSPredicate predicateWithFormat:@""];
        _dataArray = [_context executeFetchRequest:request error:nil];
        [_tableView reloadData];
        
        //满足条件的个数
        [_context countForFetchRequest:request error:nil];
    }
    - (IBAction)update:(id)sender {
        Student *model = _dataArray[_indexPath.row];
        model.name = _nameTextField.text;
        model.age = @(_ageTextField.text.integerValue);
        [_context save:nil];
    }
    - (IBAction)delete:(id)sender {
        NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Student"];
        NSArray *results = [_context executeFetchRequest:request error:nil];
        for (Student *model in results) {
            [_context deleteObject:model];
        }
        //删除所有数据,可以直接删除数据库文件
    }
    #pragma mark --UITabelViewDelegate
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return _dataArray.count;
    }
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *cellIde = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIde];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIde];
        }
        Student *student = _dataArray[indexPath.row];
        cell.textLabel.text = [NSString stringWithFormat:@"姓名:%@",student.name];
        cell.detailTextLabel.text = [NSString stringWithFormat:@"年龄:%@",student.age];
        return cell;
    }
    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        Student *model = _dataArray[indexPath.row];
        _nameTextField.text = model.name;
        _ageTextField.text = model.age.stringValue;
        _indexPath = indexPath;
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end
  • 相关阅读:
    nodejs 的序列化与反序列化
    Visual Studio 监视与快速监视即时窗口没有智能提示
    mysql 备份数据语句
    mysql 导入sql 2006
    MySql.Data.MySqlClient.MySqlException (0x80004005): Unable to connect to any of the specified MySQL hosts.
    怎么查看mysql的安装目录
    【支付宝】退款接口 报 “缺少签名参数”
    【支付宝】"验签出错,sign值与sign_type参数指定的签名类型不一致:sign_type参数值为RSA,您实际用的签名类型可能是RSA2"
    【支付宝】支付 系统繁忙,请稍后再试(ALIN10146)
    php插入日志到数据库,对象转json
  • 原文地址:https://www.cnblogs.com/liaods/p/4805573.html
Copyright © 2011-2022 走看看