繁体   English   中英

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

[英]Terminating app due to uncaught exception 'NSInvalidArgumentException'

我已经完成了自己的项目。 因此,我在模拟器上对其进行了测试,一切正常。 但不幸的是,该项目无法在我的真实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是项目简单文件之一。

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

}

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

}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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