文件管理 - 3

文件管理通常和网络编程结合一起,实现缓存化、本地化数据,网络编程篇可以参考:

http://blog.csdn.net/u013743777/article/category/5884035

在这里解析一下,如何将数据保存到本地,并用合适的装载方式 - plist文件

前面的学习篇可以知道,如果是一些长久重要的数据,我们放置于document文件夹:

- (NSString *)downloadPath:(NSString *)path{
    
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    
    NSLog(@"documentPath : %@", documentPath);
    
    NSString *downloadPath = [documentPath stringByAppendingPathComponent:path];
    
    return downloadPath;
}


并且服务器一般来说返回json为字典或者数组结构,用轻量级的XML格式plist装载就好:

- (NSString *)pathOfSTPlist:(NSString *)plist path:(NSString *)path{
    return [[self downloadPath:path] stringByAppendingPathComponent:plist];
}


并且如果我们不存在这个文件夹,我们需要创建,上一篇就有解析:

- (BOOL)createFolder:(NSString *)path{
    
    BOOL isDir = NO;
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL existed = [fileManager fileExistsAtPath:path isDirectory:&isDir];
    
    BOOL isCreated = NO;
    
    if (!(isDir == YES && existed == YES)) {
        isCreated = [fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
    }else{
        isCreated = YES;
    }
    
    return isCreated;
}



这样,我们就可以在请求服务器后将数据存在本地!

            [[LBNetworkRequestManager sharedManager] requestSActiveWithSuccess:^(id responseData){
            
                // 通常不用这么复杂,我们都知道肯定返回Dic或者Arr
                if ([responseData isKindOfClass:[NSArray class]] ||
                    [responseData isKindOfClass:[NSDictionary class]]) {
                    
                    if ([self createFolder:[self downloadPath:@"LB_S_ACTIVE"]]) {

                        if ([responseData respondsToSelector:@selector(writeToFile:atomically:)]) {
                            
                            if ([responseData writeToFile:[self pathOfSTPlist:@"LB_S_ACTIVE.plist" path:@"LB_S_ACTIVE"] atomically:YES]) {
                                
                                NSLog(@"write to file success : %@", responseData);
                            }
                        }
                    }
                }
                
            } failure:^(NSInteger statusCode, NSString *message){
            
            }];


如果我们想获取,文件内内容,可以:

    NSArray *plistArray = [NSArray arrayWithContentsOfFile:[self pathOfSTPlist]];

方法簇:

数组:

+ (nullable NSArray<ObjectType> *)arrayWithContentsOfFile:(NSString *)path;
+ (nullable NSArray<ObjectType> *)arrayWithContentsOfURL:(NSURL *)url;
- (nullable NSArray<ObjectType> *)initWithContentsOfFile:(NSString *)path;
- (nullable NSArray<ObjectType> *)initWithContentsOfURL:(NSURL *)url;


字典:

+ (nullable NSDictionary<KeyType, ObjectType> *)dictionaryWithContentsOfFile:(NSString *)path;
+ (nullable NSDictionary<KeyType, ObjectType> *)dictionaryWithContentsOfURL:(NSURL *)url;
- (nullable NSDictionary<KeyType, ObjectType> *)initWithContentsOfFile:(NSString *)path;
- (nullable NSDictionary<KeyType, ObjectType> *)initWithContentsOfURL:(NSURL *)url;





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值