错误说源路径为零

发布于 2024-12-27 13:05:13 字数 1879 浏览 0 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ζ澈沫 2025-01-03 13:05:13

设备无法写入应用程序包,您需要使用 Documents 目录。

以下是获取文档目录路径的方法:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName];

如果应用程序包中有一个初始数据库,则在初始启动时将其复制到文档中。

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:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName];

If you have an initial db in the app bundle copy it to the Documents on initial startup.

山人契 2025-01-03 13:05:13

将此行更改

NSString *databasePathFromApp=[[NSBundle mainBundle] pathForResource:@"Person.sqlite3" ofType:@"sqlite3"];

NSString *databasePathFromApp=[[NSBundle mainBundle] pathForResource:@"Person" ofType:@"sqlite3"];

也,注意大小写,iOS 设备区分大小写,而在模拟器上,您将避免大小写错误

change this line

NSString *databasePathFromApp=[[NSBundle mainBundle] pathForResource:@"Person.sqlite3" ofType:@"sqlite3"];

to

NSString *databasePathFromApp=[[NSBundle mainBundle] pathForResource:@"Person" ofType:@"sqlite3"];

also, pay attention to capitalisation, iOS devices are case sensitive, while on simulator, you'll get away with wrong capitalisation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文