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

Ios 如何将NSArray保存到文件并从文件中检索NSArray,ios,iphone,filesystems,nsarray,Ios,Iphone,Filesystems,Nsarray,我一直在努力实现在上给出的答案,但这些答案并不适用于我。有人能告诉我我做错了什么吗?我的NSArray是一个自定义对象,比如说Cat。Cat本身由字符串和数字组成。这是我的密码 -(void)saveArrayToFile:(NSArray *)response; { // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); // NSStr

我一直在努力实现在上给出的答案,但这些答案并不适用于我。有人能告诉我我做错了什么吗?我的NSArray是一个自定义对象,比如说
Cat
。Cat本身由字符串和数字组成。这是我的密码

-(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;
}
首先,我尝试了注释代码。然后我试了另一个。到目前为止,问题是没有保存该文件

如果数组的内容都是属性列表对象(NSString、NSData、NSArray或NSDictionary对象),则使用此方法编写的文件可以使用类方法arrayWithContentsOfFile:或实例方法initWithContentsOfFile:初始化新数组

这可能是如果数组包含自定义对象,则无法读取文件的原因

或者尝试使用
NSKeyedArchiver

如果数组的内容都是属性列表对象(NSString、NSData、NSArray或NSDictionary对象),则使用此方法编写的文件可以使用类方法arrayWithContentsOfFile:或实例方法initWithContentsOfFile:初始化新数组

这可能是如果数组包含自定义对象,则无法读取文件的原因


或者,在保存时尝试使用
NSKeyedArchiver

[response writeToFile:arrayPath atomicaly:YES]返回false。因此未成功保存。试用

Bool result = [NSKeyedArchiver archiveRootObject:response toFile:arrayPath];
NSArray *arrayFromFile = [NSKeyedUnarchiver unarchiveObjectWithFile:arrayPath];
在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;
}

保存时[response writeToFile:arrayPath Atomicaly:YES]返回false。因此未成功保存。试用

Bool result = [NSKeyedArchiver archiveRootObject:response toFile:arrayPath];
NSArray *arrayFromFile = [NSKeyedUnarchiver unarchiveObjectWithFile:arrayPath];
在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;
}

“NSArray必须是有效的属性列表”是什么意思?我的数组是自定义对象的集合。对象本身有字符串和数字。为什么您认为提供的代码不起作用?运行时发生了什么?属性列表对象在“无任何事件”中描述。当我试图拯救的时候,什么都没有。相反:当我尝试阅读时保存后,结果总是零。因此,在进一步研究之后,我猜想问题在于我的NSArray的内容不是所谓的“属性列表”`NSArray必须是有效的属性列表`意味着什么?我的数组是自定义对象的集合。对象本身有字符串和数字。为什么您认为提供的代码不起作用?运行时发生了什么?属性列表对象在“无任何事件”中描述。当我试图拯救的时候,什么都没有。相反:当我尝试阅读时保存后,结果总是零。因此,在进一步研究之后,我猜问题在于我的NSArray的内容不是所谓的“属性列表”。第一个片段是分别替换[response writeToFile:arrayPath Atomicaly:YES]和NSArray*arrayFromFile=[NSArray arrayWithContentsOfFile:arrayPath]。第二个代码段是添加一个名为Cat的新NSObject子类。然后您可以将响应数组设置为response=[NSArray arrayWithObjects:cat1、cat2、nil]我明白了。我无法编辑Cat对象;它来自服务器(谷歌云端点)。似乎唯一的方法是将编码器和解码器放入类中,但除了创建和使用Cat的子类之外,还有其他方法吗?我不知道您的Cat对象的结构,它是JSON格式的吗?如果你想从数组中获取信息,我认为对象映射是必要的。否。它是由谷歌的专有引擎生成的。但至少在你的帮助下,我似乎找到了问题的根源。我将继续关注如何在这方面处理Google端点。谢谢。第一个片段是分别替换[response writeToFile:arrayPath Atomicaly:YES]和NSArray*arrayFromFile=[NSArray arrayWithContentsOfFile:arrayPath]。第二个代码段是添加一个名为Cat的新NSObject子类。然后您可以将响应数组设置为response=[NSArray arrayWithObjects:cat1、cat2、nil]我明白了。我无法编辑Cat对象;它来自服务器(谷歌云端点)。似乎唯一的方法是将编码器和解码器放入类中,但除了创建和使用Cat的子类之外,还有其他方法吗?我不知道您的Cat对象的结构,它是JSON格式的吗?如果你想从数组中获取信息,我认为对象映射是必要的。否。它是由谷歌的专有引擎生成的。但至少在你的帮助下,我似乎找到了问题的根源。我将继续关注如何在这方面处理Google端点。谢谢