IOS-CoreData简单使用的主要代码<代码演示>

#import <Foundation/Foundation.h>
#import 
<CoreData/CoreData.h>
#import 
"StuName.h"

@interface CoreDataManager : NSObject

@property(nonatomic,retain)NSManagedObjectContext *managerObjectContext;//管理器
@property(nonatomic,retain)NSPersistentStoreCoordinator *persistentStoreCoordinator;//链接器
@property(nonatomic,retain)NSManagedObjectModel *managerObjectModel;//模型器

//单例方法
+(
instancetype)sharedCoreDataManager;
-(
void)saveManagerContext;
//沙盒文件路径
-(
NSURL *)applicationDocumentDirectory;
//查询单个
-(
StuName *)findStuName;
//查询所有
-(
NSArray *)findAllStuName;
//保存
-(
void)saveStuName:(StuName *)name;
//删除
-(
void)deleteStuName:(StuName *)name;

@end


#import  "CoreDataManager.h"

@implementation  CoreDataManager
#pragma mark-
#pragma mark--
单例方法
+( instancetype )sharedCoreDataManager{
    
    
static   CoreDataManager  *instance =  nil ;
    
static   dispatch_once_t  once;
    
dispatch_once (&once,^{
        
        instance = [[
self   alloc init ];
        
    });
//    if ( instance == nil) {
//        instance = [[self alloc] init];
//    }
    
    
return  instance;
}


#pragma mark-
#pragma mark----
处理的方法
-( NSArray  *)findAllStuName{

    
// 实例化一个实体的检索请求
    
NSFetchRequest  *request = [[[ NSFetchRequest   alloc initWithEntityName : @"StuName" autorelease ];
    
// 设置检索请求的关键字和排序方式、谓词等
    
NSArray  *infoList = [ self . managerObjectContext   executeFetchRequest :request  error : nil ];
    
return  infoList;
    
}
-(
void )saveStuName:( StuName  *)name{

    
// 判断管理器是否发生过变化
    
if  ([ self . managerObjectContext   hasChanges ]) {
        
// 保存
        [
self . managerObjectContext   save : nil ];
    }

}
-(
void )deleteStuName:( StuName  *)name{

    [
self . managerObjectContext   delete :name];

}
#pragma mark-
#pragma mark--
-( StuName  *)findStuName{
    
    
//1. 实例化实体对象
    
NSEntityDescription  *entityDescription = [ NSEntityDescription   entityForName : @"StuName"   inManagedObjectContext : self . managerObjectContext ];
    
    
//2. 根据实体描述对象实例化 StuName 对象
    
StuName  *info = [[[ StuName   alloc initWithEntity :entityDescription  insertIntoManagedObjectContext : self . managerObjectContext autorelease ];
    
    
return  info;
    
}

#pragma mark-- 变种的 setter Getter 方法
#pragma mark-
#pragma mark--
管理器
-( NSManagedObjectContext  *)managerObjectContext{
    
    
if  ( _managerObjectContext  ==  nil ) {
        
// 使用主线程初始化管理器
        
_managerObjectContext  = [[ NSManagedObjectContext   alloc initWithConcurrencyType : NSMainQueueConcurrencyType ];
        
// 使用链接器设置管理器
        [
_managerObjectContext   setPersistentStoreCoordinator : self . persistentStoreCoordinator ];
        
    }
    
return   _managerObjectContext ;
}
#pragma mark--
#pragma mark---
链接器
-( NSPersistentStoreCoordinator  *)persistentStoreCoordinator{
    
    
if  ( _persistentStoreCoordinator  ==  nil ) {
        
// 初始化链接器
        
_persistentStoreCoordinator  = [[ NSPersistentStoreCoordinator   alloc initWithManagedObjectModel : self . managerObjectModel ];
        
// 设置链接器的实体对象的链接方式
        [
_persistentStoreCoordinator   addPersistentStoreWithType : NSSQLiteStoreType   configuration : nil   URL : self . applicationDocumentDirectory   options : nil   error : nil ];
    }
    
    
return   _persistentStoreCoordinator ;
}


#pragma mark-
#pragma mark---
模型器
-( NSManagedObjectModel  *)managerObjectModel{
    
    
    
if  ( _managerObjectModel  ==  nil ) {
        
// 始化模型器 - 合并
        
_managerObjectModel  = [ NSManagedObjectModel   mergedModelFromBundles : nil ];
    }
    
return   _managerObjectModel ;
}

-(
void )saveManagerContext{
    
//    NSError *error = nil;
//    if (self.managerObjectContext != nil) {
//        if ([self.managerObjectContext hasChanges] && ![self.managerObjectContext save:&error]) {
//            NSLog(@" 未处理的错误信息 %@, %@", error, [error userInfo]);
//            abort();
//        }
//    }
    
if  ([ self . managerObjectContext   hasChanges ]) {
        [
self . managerObjectContext   save : nil ];
    }
    
}

#pragma mark-
#pragma mark--
重写初始化方法
-( instancetype )init{
    
    
if  ( self  = [ super   init ]) {
        
// 调用一下管理器
        [
self   managerObjectContext ];
        
// 设置实时通知中心,一旦数据发生变化就及时处理
        [[
NSNotificationCenter   defaultCenter addObserver : self   selector : @selector (saveStuName:)  name : NSManagedObjectContextObjectsDidChangeNotification   object : nil ];
        
    }
    
return   self ;
    
}
#pragma mark-
#pragma mark-----
初始化沙盒文件路径
-( NSURL  *)applicationDocumentDirectory{
    
    
NSString  *stringPath = [ NSSearchPathForDirectoriesInDomains ( NSDocumentDirectory NSUserDomainMask YES ) objectAtIndex : 0 ];
    
NSLog ( @"%@" ,stringPath);
    
NSString  *sandBoxPath = [stringPath  stringByAppendingString : @"/stuName.sqlite" ];
    
NSURL  *sandBoxUrl = [ NSURL   fileURLWithPath :sandBoxPath];
    
return  sandBoxUrl;
//    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//    NSLog(@"%@",sandBoxPath);
//    NSString *sqliteString = [sandBoxPath stringByAppendingString:@"/coredate.sqlite"];
//    
//    return [NSURL fileURLWithPath:sqliteString];
    
}

-(
void )dealloc{
    [
_managerObjectContext   release ], _managerObjectContext  =  nil ;
    [
_managerObjectModel   release ], _managerObjectModel  =  nil ;
    [
_persistentStoreCoordinator   release ], _persistentStoreCoordinator  =  nil ;
    [
super   dealloc ];
}

@end



















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QtWidgets> #include <QResizeEvent> //绘图区域大小变化事件 #include <QDebug> #include <QThread> #include <QTimer> #include <QVector> #include <QRandomGenerator> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); ui->plot->setOpenGl(true); ui->plot->setNoAntialiasingOnDrag(true); CreateChart(); } MainWindow::~MainWindow() { delete ui; } void MainWindow::CreateChart() { ui->plot->addGraph(); ui->plot->graph(0)->setPen(QPen(QColor(100,149,237))); mData = ui->plot->graph(0)->data()->coreData(); ui->plot->xAxis2->setVisible(true); ui->plot->xAxis2->setTickLabels(false); ui->plot->yAxis2->setVisible(true); ui->plot->yAxis2->setTickLabels(false); connect(ui->plot->xAxis, SIGNAL(rangeChanged(QCPRange)), ui->plot->xAxis2, SLOT(setRange(QCPRange))); connect(ui->plot->yAxis, SIGNAL(rangeChanged(QCPRange)), ui->plot->yAxis2, SLOT(setRange(QCPRange))); ui->plot->setInteractions(QCP::iRangeDrag|QCP::iRangeZoom|QCP::iSelectPlottables); ui->plot->yAxis->setRange(-1.5,1.5); ui->plot->xAxis->setRange(0,50); ui->plot->yAxis->setLabel("数据"); ui->plot->xAxis->setLabel("个数"); for (int i = 0; i < 50; i++) { mXDataTol.append(i); mYDataTol.append(QRandomGenerator::global()->bounded(1.00)); } ui->plot->graph(0)->setData(mXDataTol,mYDataTol,true); ui->plot->replot(); }这是我的全部代码,系统报错:terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc请帮我修改一下代码,我不知道到底哪里出了错
06-10

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GFanStudio-LeeSir

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值