终止应用程序由于未捕获的异常'NSInvalidArgumentException'
问题描述:
我已经完成了我自己的项目。 所以我在模拟器上测试过,一切正常。 但不幸的是,这个项目并没有运行在我真正的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];
}
答
检查,如果存在于应用程序的资源你GameInfo.plist
,它驻留在您的应用程序文件夹中(如您正在两个或更多设备上执行您的代码)。如果问题没有解决,请分享您的完整例外日志。
分享更多的代码。你在哪里使用copyItemAtPath方法? – Alin
我在读/写plist文件时使用了copyitemAtPath方法。 – bTagTiger
确保GameInfo.plist的命名就像那样。 iOS区分大小写 – Alin