美文网首页
OC CoreData持久化

OC CoreData持久化

作者: J_mine | 来源:发表于2017-09-20 19:55 被阅读0次

一 . 创建一个实体,添加一些需要的属性

屏幕快照 2017-09-20 下午7.41.57.png

接着点击Xcode上的导航栏中的Editor按钮,选中


屏幕快照 2017-09-20 下午7.45.52.png

二 创建好文件夹

屏幕快照 2017-09-20 下午7.42.45.png

三 书写代码

AppDelegate.h

#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (readonly, strong) NSPersistentContainer *persistentContainer;

- (void)saveContext;

AppDelegate.m

#import "AppDelegate.h"
#import "ViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    ViewController *theVc = [[ViewController alloc]init];
    
    UINavigationController *theNav = [[UINavigationController alloc]initWithRootViewController:theVc];
    
    self.window.rootViewController = theNav;
    
    return YES;
}

DataBase.h

#import <Foundation/Foundation.h>
#import "Entity+CoreDataClass.h"
#import "AppDelegate.h"
@interface DataBase : NSObject

+(instancetype)showdata;

-(void)addname:(NSDictionary *)dic;

-(void)changedata;

-(void)deletdata:(Entity *)theData;

-(NSMutableArray*)showAllArray;

DataBase.m

#import "DataBase.h"

static DataBase *thedatabase;
@implementation DataBase

+(instancetype)showdata{

    if (!thedatabase) {
        thedatabase = [[DataBase alloc]init];
    }
    return thedatabase;

}

-(void)addname:(NSDictionary *)dic{

    AppDelegate *app =(AppDelegate *)[UIApplication sharedApplication].delegate;
    
    Entity *person = [NSEntityDescription insertNewObjectForEntityForName:@"Entity" inManagedObjectContext:app.persistentContainer.viewContext];
    
    person.personName = dic[@"personName"];
    person.personAge = dic[@"personAge"];
    
    [app saveContext];


}

-(void)changedata{

    AppDelegate *app =(AppDelegate *)[UIApplication sharedApplication].delegate;
    
    [app saveContext];

}

-(void)deletdata:(Entity *)theData{

    AppDelegate *app =(AppDelegate *)[UIApplication sharedApplication].delegate;
    
    [app.persistentContainer.viewContext deleteObject:theData];
    
    [app saveContext];
}

-(NSMutableArray*)showAllArray{

    AppDelegate *app =(AppDelegate *)[UIApplication sharedApplication].delegate;
    
    NSFetchRequest *request = [[NSFetchRequest alloc]init];
    
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Entity" inManagedObjectContext:app.persistentContainer.viewContext];
    
    [request setEntity:entity];
    
    NSArray *arr = [app.persistentContainer.viewContext executeFetchRequest:request error:nil];
    
    return [arr mutableCopy];

}


AddView.h

#import <UIKit/UIKit.h>
@interface AddView : UIView
@property(nonatomic,strong)UITextField *theBookTF;
@property(nonatomic,strong)UITextField *thePriceTF;
@end

AddView.m

#import "AddView.h"

@implementation AddView

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        [self addSubview:self.theBookTF];
        [self addSubview:self.thePriceTF];
    }
    return self;
}
#pragma mark - 属性实例化
-(UITextField *)theBookTF
{
    if (!_theBookTF) {
        _theBookTF = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
        _theBookTF.center = CGPointMake(self.center.x, self.center.y - 50);
        _theBookTF.layer.borderWidth = 1.0;
        _theBookTF.borderStyle = UITextBorderStyleRoundedRect;
        _theBookTF.placeholder = @"姓名";
    }
    return _theBookTF;
}

-(UITextField *)thePriceTF
{
    if (!_thePriceTF) {
        _thePriceTF = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
        _thePriceTF.center = CGPointMake(self.center.x, self.center.y + 10);
        _thePriceTF.borderStyle = UITextBorderStyleRoundedRect;
        _thePriceTF.layer.borderWidth = 1.0;
        _thePriceTF.placeholder = @"年龄";
    }
    return _thePriceTF;
}
@end

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UITableViewController
@end

ViewController.m

#import "ViewController.h"
#import "DataBase.h"
#import "Entity+CoreDataClass.h"
#import "AddViewController.h"

@interface ViewController ()
@property(nonatomic,strong)NSArray *theDataArr;

@end

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"图书管理信息平台";
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"添加" style:UIBarButtonItemStylePlain target:self action:@selector(addName)];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.theDataArr = [[DataBase showdata
                        ]showAllArray];
    
    [self.tableView reloadData];
}


-(void)addName
{
    // 进入Add视图
    AddViewController *addVC = [[AddViewController alloc] init];
    
    
    [self.navigationController pushViewController:addVC animated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.theDataArr.count;
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    Entity *enti = self.theDataArr [indexPath.row];
    
    [[DataBase showdata]deletdata:enti];
    
    self.theDataArr = [[DataBase showdata]showAllArray];
    
    [self.tableView reloadData];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
    Entity *theEntity = self.theDataArr[indexPath.row];
    
    cell.textLabel.text = theEntity.personName;
    cell.detailTextLabel.text = theEntity.personAge;
    
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    AddViewController *theVc = [[AddViewController alloc]init];
    
    theVc.theEntity = self.theDataArr[indexPath.row];
    
    [self.navigationController pushViewController:theVc animated:YES];
    
}


@end

AddViewController.h

#import <UIKit/UIKit.h>
#import "Entity+CoreDataClass.h"

@interface AddViewController : UIViewController

@property(nonatomic,strong)Entity *theEntity;

AddViewController.m

#import "AddViewController.h"
#import "AddView.h"
#import "Entity+CoreDataClass.h"
#import "DataBase.h"


@interface AddViewController ()
{
    AddView *theAddView;
    
}

@end

@implementation AddViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = @"添加数据平台";
    
    
    theAddView = [[AddView alloc] initWithFrame:self.view.frame];
    theAddView.backgroundColor = [UIColor whiteColor];
    self.view = theAddView;
    
    theAddView.theBookTF.text =  self.theEntity.personName;
    
    theAddView.thePriceTF.text = self.theEntity.personAge;
    
    if (theAddView.theBookTF.text == nil ||theAddView.theBookTF.text.length <= 0)
    
        //添加按钮文字
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(save)];
    
    else
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(update)];
 
}

-(void)save
{
    
    NSDictionary *dict = @{@"personName":theAddView.theBookTF.text,@"personAge":theAddView.thePriceTF.text};
//    CoreDataModel *data = [[CoreDataModel alloc]init];
//    data.dataage = theAddView.theBookTF.text;
//    data.dataname = theAddView.thePriceTF.text;
    
    [[DataBase showdata] addname:dict];
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)update{

   self.theEntity.personName=theAddView.theBookTF.text;
    
   self.theEntity.personAge = theAddView.thePriceTF.text;
    
    [[DataBase showdata]changedata];
    
    [self.navigationController popViewControllerAnimated:YES];

}

相关文章

网友评论

      本文标题:OC CoreData持久化

      本文链接:https://www.haomeiwen.com/subject/xezosxtx.html