数据解析 持久化数据收藏

首先导入三方
创建控制器
#import <AFNetworking.h>
#import “MyTableViewCell.h”
#import “DailyModel.h”
#import “Model.h”
#import “ShouCangViewController.h”

@interface NewsViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic , strong)UITableView *tbV;
@property (nonatomic , strong)NSMutableArray *array;

@end

@implementation NewsViewController

  • (void)viewDidLoad {
    [super viewDidLoad];

    //初始化数组
    self.array = [[NSMutableArray alloc]init];

    self.view.backgroundColor = [UIColor whiteColor];

    self.navigationItem.title = @“新闻头条”;

    _tbV = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];

    _tbV.delegate = self;
    _tbV.dataSource = self;

    [self.view addSubview:self.tbV];

    //注册
    [self.tbV registerNib:[UINib nibWithNibName:@“MyTableViewCell” bundle:nil] forCellReuseIdentifier:@“cell”];

    [self loadData];

}

-(void)loadData{
AFHTTPSessionManager *manger = [AFHTTPSessionManager manager];

manger.responseSerializer = [AFHTTPResponseSerializer serializer];

[manger GET:@"http://v.juhe.cn/joke/content/list.php?key=eaaf69cdca2f46e403a264f5ef7cb74b&time=1418816972" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
    
} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:1 error:nil];
    
    NSDictionary *dic1 = [dic objectForKey:@"result"];
    
    NSDictionary *arr = [dic1 objectForKey:@"data"];
    
    for (NSDictionary *dic in arr) {
        DailyModel *model = [DailyModel new];
        
        [model setValuesForKeysWithDictionary:dic];
        
        [self.array addObject:model];
    }
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tbV reloadData];
    });
    
    
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    
    NSLog(@"请求失败");
    
    
}];

}

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

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

if (!cell) {
    cell = [[MyTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
}

_tbV.rowHeight = 200;

DailyModel *model = self.array[indexPath.row];

cell.lab1.text = model.content;

cell.lab2.text = model.updatetime;



return cell;

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

[[Model initData]addData:@{@"name":[self.array[indexPath.row] content],@"time":[self.array[indexPath.row] updatetime]}];



[self.tbV deselectRowAtIndexPath:indexPath animated:YES];

}

@end

第二个控制器

#import “Model.h”
#import “DailyModel.h”
#import “Entity+CoreDataClass.h”
@interface ShouCangViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *tab;

@property(nonatomic,strong)NSMutableArray *array;

@end

@implementation ShouCangViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];

    self.navigationItem.title = @“我的收藏”;

    self.tab=[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];

    self.tab.delegate=self;
    self.tab.dataSource=self;

    [self.view addSubview:self.tab];

    self.array=[[NSMutableArray alloc]init];
    }

//视图即将出现

-(void)viewWillAppear:(BOOL)animated{

self.array=[[Model initData]array];

[self.tab reloadData];

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

return self.array.count;

}

-(void)dian{

DailyModel *mode=[DailyModel new];


NSDictionary *dic=@{@"name":mode.content,@"time":mode.updatetime};



[[Model initData]addData:dic];

[self.navigationController popViewControllerAnimated:YES];

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *oj=@"cell";


UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:oj];


if (cell==nil) {
    cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:oj];
    
    
}

self.tab.rowHeight=200;

//    DataModel *mod=_array[indexPath.row];


Entity *cla=_array[indexPath.row];

//    DataModel *model=self.array[indexPath.row];

cell.textLabel.text=cla.name;
cell.textLabel.numberOfLines=0;
cell.detailTextLabel.text=cla.time;


return cell;

}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

//创建实体
Entity *cla=self.array[indexPath.row];

//调用删除数据的方法
[[Model initData]deleteData:cla];

//重新获取数据
self.array=[[Model initData]array];

//刷新表格
[self.tab reloadData];

}

@end

创建自定义cell
.h
#import “DailyModel.h”
NS_ASSUME_NONNULL_BEGIN

@interface MyTableViewCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UILabel *lab1;
@property (weak, nonatomic) IBOutlet UILabel *lab2;
-(void)setDataModel:(DailyModel *)model;

.m

  • (void)setDataModel:(DailyModel *)model{

    if (model) {

      self.lab1.text=model.content;
      self.lab2.text=model.updatetime;
    

    }

}

创建DailyModel
.h
@property(nonatomic,strong)NSString *content;
@property(nonatomic,strong)NSString *updatetime;
@property(nonatomic,strong)NSString *hashId;
.m
-(void)setValue:(id)value forUndefinedKey:(NSString *)key{

}

数据化 model
.h
#import “AppDelegate.h”
#import “Entity+CoreDataClass.h”

NS_ASSUME_NONNULL_BEGIN

@interface Model : NSObject{
AppDelegate *app;
}

//创建单利方法

//单利方法
+(instancetype)initData;
//添加数据
-(void)addData:(NSDictionary * )dic;
//删除数据
-(void)deleteData:(Entity *)data;
//修改
-(void)upData;
//查询
-(NSMutableArray *)array;

@end

.m

#import “DailyModel.h”

//创建静态变量
static Model *da;
@implementation Model

+(instancetype)initData{
if (!da) {
da = [Model new];
}

return da;

}

-(void)addData:(NSDictionary *)dic{
//创建appdelegate对象
app = (AppDelegate *)[[UIApplication sharedApplication] delegate];

//创建实体对象  NSEntityDescription(实体描叙对象)
Entity *cla = [NSEntityDescription insertNewObjectForEntityForName:@"Entity" inManagedObjectContext:app.persistentContainer.viewContext];

cla.name=dic[@"name"];
cla.time=dic[@"time"];
//
[app saveContext];

}

//删除数据

  • (void)deleteData:(Entity *)data{

    //创建appdelegate对象
    app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    //删除
    [app.persistentContainer.viewContext deleteObject:data];
    //保存数据
    [app saveContext];

}

//修改数据

  • (void)upData{

    //创建appdelegate对象
    app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    //保存数据
    [app saveContext];

}

//查询数据

  • (NSMutableArray *)array{

    //创建appdelegate对象
    app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    //创建请求数据对象
    NSFetchRequest *requst = [[NSFetchRequest alloc] init];
    //创建实体描述类
    NSEntityDescription *ent = [NSEntityDescription entityForName:@“Entity” inManagedObjectContext:app.persistentContainer.viewContext];
    //获取数据
    [requst setEntity:ent];
    //从实体中获取数据
    NSArray *arr = [app.persistentContainer.viewContext executeFetchRequest:requst error:nil];

    return [arr mutableCopy];

}

创建实体
info.plist 开通网络

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。
Redis是一种高性能的key-value存储系统,它通过内存中的数据结构来存储数据,因此具有非常快的读写速度。然而,由于数据存储在内存中,一旦Redis服务器重启或崩溃,所有数据将会丢失。为了解决这个问题,Redis提供了持久化机制,将数据持久化到磁盘上,以确保数据的安全性。 Redis的持久化机制有两种形式,一种是RDB持久化,另一种是AOF持久化。 RDB持久化是将Redis的内存数据以快照的形式保存到磁盘上。它通过fork子进程来实现持久化操作,在子进程中将当前数据集的副本写入磁盘,完成之后再替换原有的RDB文件。这种方式具有很高的性能,能够在短时间内完成大量的数据写入,同时也不会影响Redis服务器的正常操作。另外,RDB文件是二进制格式的,非常紧凑,可以有效地减少磁盘占用空间。 AOF持久化则是将Redis的每个写命令追加到AOF文件末尾,以日志的形式记录下来。当Redis重启时,它会重新执行AOF文件中的所有写命令,将数据恢复到原始状态。这种方式能够提供更好的数据安全性,因为它是一个追加写入的操作,不会影响到已有的数据。此外,AOF文件是一个文本文件,易于被其他程序读取和解析。 通过RDB和AOF这两种持久化机制,Redis能够保证数据的安全性。即使在服务器宕机或重启的情况下,也能够快速恢复数据。同时,Redis还提供了多种备份和灾难恢复的方案,如定期备份、主从复制等,进一步提高了数据的可靠性和安全性。 总之,Redis的持久化机制保证了数据的安全性,通过将数据持久化到磁盘上,即使在重启或崩溃的情况下也能够快速恢复数据,提高了系统的可靠性和稳定性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值