简体   繁体   中英

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

}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM