Search code examples
iphoneobjective-cxcode4cocos2d-iphone

Terminating app due to uncaught exception 'NSInvalidArgumentException'


I've finished my own project. So I tested it on the simulator and everything is ok. But unfortunately, the project does not run on my real iphone. This error occurs:

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

What is the reason? Here is some code that I used:

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

RootViewController is one of 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];

}


Solution

  • check if your GameInfo.plist exist in your application's resource, and it reside inside your application folder(if you are executing your code on two or more devices). If problem doesn't get solved, share your, complete exception log.