文件管理 - 3

来源:互联网 发布:android软件培训 编辑:程序博客网 时间:2024/06/06 05:01

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

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
原创粉丝点击