繁体   English   中英

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

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

我一直在尝试实现给出的答案, 是否可以将NSMutableArray或NSDictionary数据另存为iOS中的文件? 他们还没有为我工作。 有人可以告诉我我做错了什么吗? 我的NSArray是一个自定义对象,例如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;
}

首先,我尝试了注释的代码。 然后我尝试了另一个。 到目前为止的问题是该文件没有被保存。

保存时[以原子方式响应writeToFile:arrayPath: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;
}

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

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

或者尝试使用NSKeyedArchiver

暂无
暂无

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

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