簡體   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