2011-09-21 129 views
2

我已經完成了我自己的項目。 所以我在模擬器上測試過,一切正常。 但不幸的是,這個項目並沒有運行在我真正的iphone上。發生此錯誤:終止應用程序由於未捕獲的異常'NSInvalidArgumentException'

"Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSFileManager copyItemAtPath:toPath:error:]: source path is nil'" 

是什麼原因? 下面是一些代碼,我用:

RootViewController *rootViewController; 
... 
- (void) startApp; 
{ 
    [rootViewController init];   // error occured 
} 

RootViewController的是項目的簡單文件之一。

void Game::readData() 
{ 

    NSError *error; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //1 
    NSString *documentsDirectory = [paths objectAtIndex:0]; //2 
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"GameInfo.plist"]; //3 

    NSFileManager *fileManager = [NSFileManager defaultManager]; 

    if (![fileManager fileExistsAtPath: path]) //4 
    { 
     NSString *bundle = [[NSBundle mainBundle] pathForResource:@"GameInfo" ofType:@"plist"]; 

     [fileManager copyItemAtPath:bundle toPath: path error:&error]; //6 
    } 

    NSMutableDictionary *savedStock = [[NSMutableDictionary alloc] initWithContentsOfFile: path]; 

    //load from savedStock example int value 
    gameMaxLevel = [[savedStock objectForKey:@"GameMaxLevel"] intValue]; 

    [savedStock release]; 

} 

無效遊戲:: READDATA() {

NSError *error; 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //1 
NSString *documentsDirectory = [paths objectAtIndex:0]; //2 
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"GameInfo.plist"]; //3 

NSFileManager *fileManager = [NSFileManager defaultManager]; 

if (![fileManager fileExistsAtPath: path]) //4 
{ 
    NSString *bundle = [[NSBundle mainBundle] pathForResource:@"GameInfo" ofType:@"plist"]; 

    [fileManager copyItemAtPath:bundle toPath: path error:&error]; //6 
} 

NSMutableDictionary *savedStock = [[NSMutableDictionary alloc] initWithContentsOfFile: path]; 

//load from savedStock example int value 
gameMaxLevel = [[savedStock objectForKey:@"GameMaxLevel"] intValue]; 

[savedStock release]; 

}

+0

分享更多的代碼。你在哪裏使用copyItemAtPath方法? – Alin

+0

我在讀/寫plist文件時使用了copyitemAtPath方法。 – bTagTiger

+1

確保GameInfo.plist的命名就像那樣。 iOS區分大小寫 – Alin

回答

0

檢查,如果存在於應用程序的資源你GameInfo.plist,它駐留在您的應用程序文件夾中(如您正在兩個或更多設備上執行您的代碼)。如果問題沒有解決,請分享您的完整例外日誌。

相關問題