错误说源路径为零
我对 iOS 应用程序开发还很陌生。
我正在尝试构建一个示例数据库应用程序,它将记录保存到数据库并从中获取。
当我用模拟器测试时,该应用程序运行良好。我已经创建了一个本地数据库,并且在需要时以编程方式制作它的副本。我还检查了“复制捆绑资源”中是否引用了本地数据库。
但我收到的错误是“* 由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'* -[NSFileManager copyItemAtPath:toPath:error:]:源路径为零'”。
我认为导致这个问题的代码是 “ [FileManager copyItemAtPath:databasePathFromApp toPath:DBPath error:nil]; “
它对于模拟器来说非常好,但当我用我的设备进行测试时却不行。
期待帮助。
我的代码是,
- (id)init {
self = [super init];
//Datbase Name
DBName=@"Person.sqlite3";
//Getting DB Path
NSArray *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentsPath objectAtIndex:0];
DBPath=[documentsDir stringByAppendingPathComponent:DBName ];
//DBPath = [[NSString alloc] initWithString: [documentsDir stringByAppendingPathComponent:DBName]];
NSLog(@"DBPath is %@",DBPath);
return self;
}
-(void)checkAndCreateDB{
BOOL Success;
//NSFileManager maintains File
NSFileManager *FileManager = [NSFileManager defaultManager];
NSLog(@"line 1");
//Checks Database Path
Success = [FileManager fileExistsAtPath:DBPath];
NSLog(@"line 2");
//If file exists, it returns true
if(Success)return;
NSLog(@"line 3");
//NSString *databasePathFromApp = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:DBName];
// Get the path to the database in the application package
NSString *databasePathFromApp=[[NSBundle mainBundle] pathForResource:@"Person.sqlite3" ofType:@"sqlite3"];
NSLog(@"line 4");
[FileManager copyItemAtPath:databasePathFromApp toPath:DBPath error:nil];
//[FileManager release];
NSLog(@"line 5");
}
在 NSLOG(第 4 行)行之后在设备中进行测试时,我的应用程序崩溃了;但在模拟器中效果很好。
非常感谢 普拉卡什
I am quite new to iOS app development.
I am trying to build a sample DB app which saves records to DB and fetches from it.
The App works well when I test with simulators. I have created a local database and also I am programatically making a copy of it when required. Also I have checked that local DB is referenced in the 'Copy Bundle Resources'.
But the error I am getting is, " * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSFileManager copyItemAtPath:toPath:error:]: source path is nil' ".
I think the piece of code which causes this problem is
" [FileManager copyItemAtPath:databasePathFromApp toPath:DBPath error:nil]; "
It work perfectly good for simulators but not when I test with my device.
Expecting help.
My code is,
- (id)init {
self = [super init];
//Datbase Name
DBName=@"Person.sqlite3";
//Getting DB Path
NSArray *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentsPath objectAtIndex:0];
DBPath=[documentsDir stringByAppendingPathComponent:DBName ];
//DBPath = [[NSString alloc] initWithString: [documentsDir stringByAppendingPathComponent:DBName]];
NSLog(@"DBPath is %@",DBPath);
return self;
}
-(void)checkAndCreateDB{
BOOL Success;
//NSFileManager maintains File
NSFileManager *FileManager = [NSFileManager defaultManager];
NSLog(@"line 1");
//Checks Database Path
Success = [FileManager fileExistsAtPath:DBPath];
NSLog(@"line 2");
//If file exists, it returns true
if(Success)return;
NSLog(@"line 3");
//NSString *databasePathFromApp = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:DBName];
// Get the path to the database in the application package
NSString *databasePathFromApp=[[NSBundle mainBundle] pathForResource:@"Person.sqlite3" ofType:@"sqlite3"];
NSLog(@"line 4");
[FileManager copyItemAtPath:databasePathFromApp toPath:DBPath error:nil];
//[FileManager release];
NSLog(@"line 5");
}
My Application crashes while testing in device after the line NSLOG(line 4); But works good in simulator.
Many Thanks
Prakash
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
设备无法写入应用程序包,您需要使用 Documents 目录。
以下是获取文档目录路径的方法:
如果应用程序包中有一个初始数据库,则在初始启动时将其复制到文档中。
The device can not write to the app bundle, you need to use the Documents directory.
Here is how to get the Documents directory path:
If you have an initial db in the app bundle copy it to the Documents on initial startup.
将此行更改
为
也,注意大小写,iOS 设备区分大小写,而在模拟器上,您将避免大小写错误
change this line
to
also, pay attention to capitalisation, iOS devices are case sensitive, while on simulator, you'll get away with wrong capitalisation