数据解析 持久化数据收藏

本文深入探讨了iOS开发中的关键技术和实践,包括Xcode的高效使用、Swift语言特性、UIKit框架详解、Auto Layout布局策略以及如何优化App性能,适合iOS开发者进阶学习。

首先导入三方
创建控制器
#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 开通网络

### Linux 创建目录命令 `mkdir` 示例与用法 在 Linux 系统中,`mkdir` 是用于创建目录命令。以下是该命令的详细用法和示例。 #### 基本语法 ```bash mkdir [选项] 目录名 ``` #### 选项说明 - `-m`:手动配置所创建目录的权限,而不是使用默认权限[^3]。 - `-p`:递归创建所有必要的父目录。如果目标目录已经存在,则不会报错[^3]。 #### 示例 1. **创建单个目录** 使用以下命令可以创建一个名为 `test` 的目录: ```bash mkdir test ``` 2. **创建多级目录** 若要一次性创建多级目录(例如 `/home/test/demo`),可以使用 `-p` 选项: ```bash mkdir -p /home/test/demo ``` 这条命令会自动创建 `/home`、`/home/test` 和 `/home/test/demo`,即使某些父目录不存在也不会报错[^3]。 3. **指定目录权限** 在创建目录时,可以通过 `-m` 选项设置目录的权限。例如,创建一个权限为 `755` 的目录: ```bash mkdir -m 755 mydir ``` 这里的 `755` 表示目录的所有者具有读、写和执行权限,而组用户和其他用户仅具有读和执行权限[^3]。 4. **批量创建多个目录** 可以通过一次调用 `mkdir` 命令创建多个目录。例如: ```bash mkdir dir1 dir2 dir3 ``` 这条命令将同时创建 `dir1`、`dir2` 和 `dir3` 三个目录[^2]。 #### 注意事项 - 如果尝试创建目录已经存在且未使用 `-p` 选项,系统会返回错误提示。 - 使用 `-m` 选项时,权限设置会覆盖默认的 umask 设置。 ### 实际应用场景 假设需要在一个项目中创建一个多层目录结构来存放代码和文档: ```bash mkdir -p project/{src,docs,tests} ``` 这条命令创建 `project` 目录及其子目录 `src`、`docs` 和 `tests`。花括号 `{}` 用于简化批量创建操作[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值