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