2009-06-08 111 views
4

我有一個簡單的應用程序試圖使用NSKeyedArchiver/NSKeyedUnarchiver在調用之間保存一些數據。它在模擬器上工作正常,但是當我將它導出到真正的iPhone硬件時,NSKeyedArchiver無法寫入磁盤。NSKeyedArchiver在iPhone模擬器中工作,在設備上失敗

我使用的是類方法

[的NSKeyedArchiver archiveRootObject:myRootObject TOFILE:@ 「data.archive」]

所有我的 'myRootObject' 對象實現NSCoding協議。再次,在模擬器上,這返回YES,一切都很好。在設備上,這將返回NO並且沒有保存。 (都是2.2.1)

是否有一些寫入權限或應用程序需要能夠寫入手機的HD?有人看過這個嗎?

感謝您的幫助提前。

回答

10

我在這裏猜測,但不是你指定文件路徑指向你的包內的目錄? 我認爲你可以寫入你的NSBundle下的Documents目錄。

嘗試創建這樣的文件路徑:

// Documents directory 
NSArray *paths = 
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
            NSUserDomainMask, YES); 
NSString *documentsPath = [paths objectAtIndex:0]; 

// <Application Home>/Documents/foo.arch 
NSString *fooPath = 
[documentsPath stringByAppendingPathComponent:@「foo.arch」]; 

再經過「fooPath」爲路徑,在你使用的方法您的文件,看看是否有幫助。

1

感謝您的幫助,您讓我走上了正確的思路......訣竅是弄清楚應用程序的根目錄,並寫入該目錄的子目錄。煩惱模擬器讓你隨時隨地寫信。

這裏是固定的一切:

NSString *localPath = @"Documents/my.archive"; 
NSString *fullPath = [NSHomeDirectory() stringByAppendingPathComponent:localPath]; 
[NSKeyedArchiver archiveRootObject:archive toFile:fullPath]; 

參見本similar thread

+0

好,你想通了,我覺得NSSearchPathForDirectoriesInDomains會給你的文檔目錄中。 – stefanB 2009-06-08 07:05:03

相關問題