這是我的代碼..爲什麼它不會在設備上添加對象數組? 也許陣列不能從文件讀取?我將文件寫入文檔目錄,但可能會出錯?謝謝...在模擬器上工作,無法在iPhone設備上工作
if (contactsToProcess==nil)
{
contactsToProcess=[[NSMutableArray alloc] init];
NSLog(@"Array with objs initialized");
}
contactsToProcess=[NSKeyedUnarchiver unarchiveObjectWithFile:fPath()];
[contactsToProcess addObject:cont];
NSLog(@"Object added to file:");
for(Contact *cn in contactsToProcess)
{
NSLog(@"%@", cn.fName);
}
[NSKeyedArchiver archiveRootObject:contactsToProcess toFile:fPath()];
fPath()的作用:
NSString* PathforResources(NSString* filename)
{
NSString *leftpart=[filename stringByDeletingPathExtension];
NSString *extension=[filename pathExtension];
return [[NSBundle mainBundle] pathForResource:leftpart ofType:extension];
}
NSString* DocumentsDirectory()
{
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDirectory, YES);
return [paths objectAtIndex:0];
}
NSString* fPath()
{
return [DocumentsDirectory() stringByAppendingPathComponent:@"quickdial.xml"];
}
前6行('如果contactsToProcess == nil {...}')無用,因爲您在第6行重新指定了「contactsToProcess」。 – Cyrille
'fPath()'返回的路徑是什麼? – PeyloW
它返回Documents目錄的路徑:NSString * PathforResources(NSString * filename) { \t NSString * leftpart = [filename stringByDeletingPathExtension]; \t NSString * extension = [filename pathExtension]; \t return [[NSBundle mainBundle] pathForResource:leftpart ofType:extension]; \t } 的NSString * DocumentsDirectory() { \t的NSArray *路徑= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDirectory,YES); \t return [paths objectAtIndex:0]; \t } 的NSString * fPath() { \t返回[DocumentsDirectory()stringByAppendingPathComponent:@ 「quickdial.xml」]; } –