由于未捕获的异常'NSInvalidArgumentException'而终止应用程序

时间:2011-09-21 13:27:54

标签: iphone objective-c xcode4 cocos2d-iphone

我已经完成了自己的项目。 所以我在模拟器上测试了它,一切都很好。 但不幸的是,该项目并没有在我真正的iphone上运行。发生此错误:

"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是Project Simple File之一。

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];

}

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];

}

1 个答案:

答案 0 :(得分:0)

检查应用程序资源中是否存在GameInfo.plist,并且它位于应用程序文件夹中(如果您在两个或多个设备上执行代码)。如果问题无法解决,请分享您的完整例外日志。