Plist数据持久化

AppDelegate.m里
导头文件
#import "ViewController.h"
//导航栏

self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[[ViewController alloc] init]];

ViewController.h

#import <UIKit/UIKit.h>
typedef void(^block_send)(id block_Send);
@interface ViewController : UIViewController

+(void)sendWithblock:(block_send) blockSend;
@end

ViewController.m里
头文件

#import "dataWithGET.h"
<UITableViewDelegate,UITableViewDataSource>
{
    UITableView *tb_View;
    NSMutableArray *arrRow;
    NSString *filePath;
    NSMutableDictionary *data;
    
}

viewDidLoad

self.title = @"wdaw";
    
    
    //实例方法用类名调用
   
   
 
    tb_View = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    
    tb_View.delegate = self;
    tb_View.dataSource = self;
    [self.view addSubview:tb_View];
    [self strin];
    NSLog(@"%@",arrRow);
    [self write];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"dwa" style:UIBarButtonItemStyleDone target:self action:@selector(d)];
-(void)write{
    //写入plist
  
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return arrRow.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    cell.textLabel.text = arrRow[indexPath.row];
    
    
    
    return cell;
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // 1.删除数据
        
        NSInteger row = indexPath.row;
        [arrRow removeObjectAtIndex:row];
        data = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];
        NSLog(@"%@", arrRow);
        // 添加一项内容
        
        [data setObject:arrRow forKey:@"c_key"];
        NSLog(@"%@",data);
        if ([arrRow writeToFile:filePath atomically:YES]) {
            NSLog(@"写入成功");
        } else {
            NSLog(@"写入失败");
        }
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        
    }
    
    

}
-(void)strin{
    arrRow = [[NSMutableArray alloc] init];
    [dataWithGET getSting:@"http://live.ximalaya.com/live-web/v4/homepage?device=iPhone" block:^(id  _Nonnull block) {
//        NSLog(@"%@",block);
        NSDictionary *dic = block;
        
//        NSLog(@"%@",dic[@"data"][@"categories"]);
        NSDictionary *dicName = dic[@"data"][@"categories"];
        
        for (id w in dicName) {
            

            [self->arrRow addObject:w[@"name"]];
        }
        
        NSArray *pathArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
        NSLog(@"%@",pathArr);
        NSString *path = [pathArr objectAtIndex:0];
        //得到完整的文件名
        filePath = [path stringByAppendingPathComponent:@"test.plist"];
        
        data = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];
        NSLog(@"%@", arrRow);
        // 添加一项内容
        
        [data setObject:arrRow forKey:@"ckey"];
        
        // 文件写入
        if ([arrRow writeToFile:filePath atomically:YES]) {
            NSLog(@"写入成功");
        } else {
            NSLog(@"写入失败");
        }
        
        
        // 那怎么证明我的数据写入了呢?读出来看看
        // 文件读取
        NSMutableDictionary *data1 = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
        NSLog(@"————————————%@", data1);
        NSLog(@"%@",self->arrRow);
        [self->tb_View reloadData];
        
    } Block:^(id  _Nonnull Block) {
        NSLog(@"%@",Block);
    }];
}

创建一个继承于NSObject的类 名为dataWithGET
.h

#import <Foundation/Foundation.h>
#import "AFNetworking.h"
NS_ASSUME_NONNULL_BEGIN
typedef void(^block_Get)(id block);
typedef void(^block_error)(id Block);

@interface dataWithGET : NSObject

+(void)getSting:(NSString *)str block:(block_Get)block Block:(block_error)Block;

@end

NS_ASSUME_NONNULL_END

.m

#import "dataWithGET.h"

@implementation dataWithGET
+(void)getSting:(NSString *)str block:(block_Get)block Block:(block_error)Block{
    
    AFHTTPSessionManager *manage = [AFHTTPSessionManager manager];
    [manage GET:@"http://live.ximalaya.com/live-web/v4/homepage?device=iPhone" parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"请求成功");
        block(responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"请求失败");
        Block(error);
        
    }];
    
}
@end

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值