是否可以在iOS中将NSMutableArray保存到磁盘?

时间:2014-08-30 10:56:39

标签: ios iphone nsmutablearray

是否可以在iOS中将NSMutableArray保存到磁盘?如果是这样你会怎么做呢?

我尝试了以下但没有成功。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:@"test.plist" ];
[assets writeToFile:path atomically:YES];

我的NSMutableArray包含从ALAssetsLibrary检索到的照片和视频的ALAssets,如下所示:

ALAssetsGroupEnumerationResultsBlock assetsEnumerationBlock = ^(ALAsset *result, NSUInteger index, BOOL *stop) { if (result) { [assets addObject:result]; } };

1 个答案:

答案 0 :(得分:3)

你可以吼叫

NSMutableArray *assets = [[NSMutableArray alloc] init];
    NSMutableDictionary *aDict = [[NSMutableDictionary alloc] init];
    [aDict setValue:@"Rajesh" forKey:@"Name"];
    [aDict setValue:@"India" forKey:@"Country"];
    [assets addObject:aDict];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString* path = [documentsDirectory stringByAppendingPathComponent:@"test.plist" ];
    [NSKeyedArchiver archiveRootObject:assets toFile:path];
    NSMutableArray *assetsBack = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    NSLog(@"%@", assetsBack);