2013-01-07 128 views
0

在我的遊戲中,我將玩家的統計信息保存在存儲在Documents目錄中的plist中。我有一個應該保存的名爲「Default_Stats.plist」的每個統計數據的空字典,以便如果這是第一次加載應用程序,它會將其複製到適當的目錄中,以便隨時加載和覆蓋。問題是,每次我的應用程序加載時,它都不會識別「Stats.plist」並用默認統計信息覆蓋它,重新設置玩家所做的每個統計信息......而且很奇怪,它完全適用於模擬器,但不在設備上。這裏是我的代碼:FileExistAtPath在模擬器上工作,但在設備上不工作

在這種方法我的統計:

- (void) readStatsFromFile{ 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *statsPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Stats.plist"]; 

    //Check if the file has already been created 
    if (![[NSFileManager defaultManager] fileExistsAtPath:statsPath]){ 
     [self createStatsList]; 
    }else{ 
     stats = [[NSMutableDictionary dictionaryWithContentsOfFile:statsPath]retain]; 
    } 
} 

這裏是我的創建方法:

- (void) createStatsList{ 
    NSString *statsPath = [[NSBundle mainBundle] bundlePath]; 
    statsPath = [statsPath stringByAppendingPathComponent:@"Default_Stats.plist"]; 

    stats = [[NSMutableDictionary dictionaryWithContentsOfFile:statsPath] retain]; 

    [self writeStatsToFile]; 
} 

而且我方法:

- (void) writeStatsToFile{ 
    BOOL ok; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *statsPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Stats.plist"]; 
    ok = [stats writeToFile:statsPath atomically:YES]; 

    if (!ok) { 
     NSLog(@"Couldn't write to file"); 
    }else 
     NSLog(@"Stats written succesfully!"); 
} 

請幫忙,我真的不明白有什麼問題!我希望我已經讓自己清楚了!

回答

0

使用文件路徑而不是絕對路徑。

+0

你能更具體嗎? – RaphBlanchet

0

您的mac中可能存在重複項,這會使模擬器上的exists = true,而不是設備上。

最簡單的檢查方法是NSLog遇到的路徑。請參閱these tools - 它們允許捕獲控制檯日誌以獲取設備上運行的發佈版本。

0

您的文檔目錄很可能不存在 - 在模擬器上,您與Mac上的所有人共享文檔目錄;在設備上,每個人都有自己的目錄。使用文件管理器方法

createDirectoryAtURL:url withIntermediateDirectories:YES 

確保該目錄在您嘗試寫入之前存在。 (我傾向於使用URL方法而不是文件路徑方法)。

PS。我建議讓一個方法返回你想要的路徑或url。不要一次又一次複製你的代碼是一個好習慣。

0

我會做很多漂亮的,像在一個會話中的一切:

  1. 得到URL文件文件夾中的文件;
  2. 如果該文件尚未存在,則將該文件從複製到文檔文件夾;

,應該是對的方法,我已經定義了一些宏,以避免在代碼中錯誤輸入該文件的名稱:

- (NSURL *)statsFileURL { 
#define NSStringFromFileNameWithExtension(filename, extension) [(filename) stringByAppendingPathExtension:(extension)] 
#define kExtension @"plist" 
#define kDefaultStatsFileName @"Default_Stats" 
#define kCustomStatsFileName @"Stats" 

    NSURL *_returnURL = nil; 

    NSFileManager *_fileManager = [NSFileManager defaultManager]; 
    NSURL *_documentDirectory = [[_fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; 
    NSURL *_myFileURLInDocumentFolder = [_documentDirectory URLByAppendingPathComponent:NSStringFromFileNameWithExtension(kDefaultStatsFileName, kExtension)]; 
    if ([_fileManager fileExistsAtPath:[_myFileURLInDocumentFolder path]]) { 
     _returnURL = _myFileURLInDocumentFolder; 
    } else { 
     NSURL *_myFileURLInBundle = [[NSBundle mainBundle] URLForResource:kDefaultStatsFileName withExtension:kExtension]; 
     if ([_fileManager fileExistsAtPath:[_myFileURLInBundle path]]) { 
      NSError *_error = nil; 
      if ([_fileManager copyItemAtURL:_myFileURLInBundle toURL:_myFileURLInDocumentFolder error:&_error]) { 
       if (_error == nil) { 
        _returnURL = _myFileURLInDocumentFolder; 
       } else { 
        // some error during copying 
       } 
      } else { 
       // some error during copying 
      } 
     } else { 
      // the file does not esixts at all, not even in the bundle 
     } 
    } 

    return _returnURL; 
} 

URL總是文檔文件夾中指出,所以您將擁有對文件的讀/寫訪問權限 - 如果發生某些錯誤,將會是nil

在您擁有URL之後,您可以在沒有任何問題的情況下恢復到文件,並且在運行時的其他時間點,您可以隨時覆蓋文件以方便您使用。


注:您可能需要擴展該代碼更詳細的錯誤處理,我把意見,只有當你需要擔心潛在錯誤的地方。

相關問題