2014-09-11 78 views
1

我一直在試圖實現在is it possible to save NSMutableArray or NSDictionary data as file in iOS?給出的答案,他們一直沒有爲我工作。有人會告訴我我做錯了什麼嗎?我的NSArray是一個自定義對象,例如Cat。貓本身是由字符串和數字組成的。這裏是我的代碼如何保存NSArray並從文件中檢索NSArray

-(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; 
} 

首先我嘗試了註釋的代碼。然後我嘗試了另一個。目前的問題是文件沒有被保存。

+0

什麼的'NSArray的必須是一個有效的屬性lists'是什麼意思?我的數組是自定義對象的集合。該對象本身有字符串和數字。 – 2014-09-11 01:47:22

+0

爲什麼你認爲提供的代碼不起作用?當你運行它時發生了什麼? – Jonah 2014-09-11 01:54:22

+0

屬性列表對象在https://developer.apple.com/library/mac/documentation/general/conceptual/devpedia-cocoacore/PropertyList.html – Jonah 2014-09-11 01:56:42

回答

1

如果陣列中的內容都是屬性列表對象(的NSString,NSData的,NSArray的,或者NSDictionary的對象),此方法寫入的文件可以被用來初始化類方法arrayWithContentsOfFile一個新的數組:或實例方法initWithContentsOfFile :.

這可能是爲什麼,如果你的數組包含自定義對象

或者嘗試使用NSKeyedArchiver

4

當你保存它[響應將writeToFile您無法讀取該文件的原因:arrayPath原子:YES ]返回false。所以它不能成功保存。嘗試使用

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

在你的貓文件:

@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; 
} 
+0

第一個片段是替換你的[響應writeToFile:arrayPath atomically:YES]和NSArray * arrayFromFile = [NSArray arrayWithContentsOfFile: arrayPath]。第二個片段是添加一個名爲Cat的新NSObject子類。然後你可以設置你的響應數組爲response = [NSArray arrayWithObjects:cat1,cat2,nil] – gabbler 2014-09-11 04:37:51

+0

我明白了。我無法編輯Cat對象;它來自服務器(Google Cloud Endpoint)。看來唯一的方法是讓編碼器和解碼器進入類,但除了創建和使用Cat的子類之外,還有其他方法嗎? – 2014-09-11 04:44:29

+0

我不知道你的Cat對象的結構是JSON格式嗎?如果你想從你的數組中獲取信息,我認爲對象映射是必須的。 – gabbler 2014-09-11 04:53:27