简体   繁体   English

如何将NSArray保存到文件并从文件中检索NSArray

[英]How to save NSArray to and retrieve NSArray from file

I have been trying to implement the answers given at is it possible to save NSMutableArray or NSDictionary data as file in iOS? 我一直在尝试实现给出的答案, 是否可以将NSMutableArray或NSDictionary数据另存为iOS中的文件? and they have not been working for me. 他们还没有为我工作。 Will someone please inform me of what I am doing wrong? 有人可以告诉我我做错了什么吗? My NSArray is of a custom object, say Cat . 我的NSArray是一个自定义对象,例如Cat Cat itself is constituted of strings and numbers. 猫本身由字符串和数字组成。 Here is my code 这是我的代码

-(void)saveArrayToFile:(NSArray *)response;
{
//    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//    NSString *documentsDirectory = [paths objectAtIndex:0];
//    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:PERSISTENT_FILENAME];
//    
//    [response writeToFile:filePath atomically:YES];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    if ([paths count] > 0)
    {
        // Path to save array data
        NSString  *arrayPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"array.out"];
        // Write array
        [response writeToFile:arrayPath atomically:YES];


    }else NSLog(@"NO paths");
}

-(NSArray *)getArrayFromFile
{
//    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//    NSString *documentsDirectory = [paths objectAtIndex:0];
//    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:PERSISTENT_FILENAME];
//    return [NSArray arrayWithContentsOfFile:filePath];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    if ([paths count] > 0)
    {
        // Path to save array data
        NSString  *arrayPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"array.out"];
        NSArray *arrayFromFile = [NSArray arrayWithContentsOfFile:arrayPath];
        return arrayFromFile;
    }else NSLog(@"No file to retrieve");
    return nil;
}

First I tried the commented code. 首先,我尝试了注释的代码。 Then I tried the other. 然后我尝试了另一个。 The problem so far is that the file is not being saved. 到目前为止的问题是该文件没有被保存。

When you save it [response writeToFile:arrayPath atomically:YES] return false. 保存时[以原子方式响应writeToFile:arrayPath:YES]返回false。 So it is not saved successfully. 因此无法成功保存。 Try use 尝试使用

Bool result = [NSKeyedArchiver archiveRootObject:response toFile:arrayPath];
NSArray *arrayFromFile = [NSKeyedUnarchiver unarchiveObjectWithFile:arrayPath];

In your cat file: 在您的cat文件中:

@property(nonatomic, strong)NSString* score;
@property(nonatomic, assign)BOOL bGenuine;
@property(nonatomic, assign)NSInteger errorCode;

@synthesize score,bGenuine,errorCode;

- (void)encodeWithCoder:(NSCoder *)encoder {
    [encoder encodeObject:score forKey:@"score"];
    [encoder encodeObject:[NSNumber numberWithBool:bGenuine] forKey:@"bGenuine"];
    [encoder encodeObject:[NSNumber numberWithInt:errorCode] forKey:@"errorCode"];
}

- (id)initWithCoder:(NSCoder *)decoder {
    score = [decoder decodeObjectForKey:@"score"];
    bGenuine = [[decoder decodeObjectForKey:@"bGenuine"] boolValue];
    errorCode = [[decoder decodeObjectForKey:@"errorCode"] integerValue];
    return self;
}

If the array's contents are all property list objects (NSString, NSData, NSArray, or NSDictionary objects), the file written by this method can be used to initialize a new array with the class method arrayWithContentsOfFile: or the instance method initWithContentsOfFile:. 如果数组的内容都是所有属性列表对象(NSString,NSData,NSArray或NSDictionary对象),则此方法编写的文件可用于使用类方法arrayWithContentsOfFile:或实例方法initWithContentsOfFile:初始化新数组。

This might be the reason why you can't read the file if your array contains custom objects 这可能是为什么如果数组包含自定义对象则无法读取文件的原因

Alternatively try to use NSKeyedArchiver 或者尝试使用NSKeyedArchiver

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM