iphone 判断文件是否存在,如果不存在就从Bundle里面读取
用于把数据文件打包到工程中,然后在运行的时候判断软件的沙盒中是否有数据,如果有数据就不拷贝,如果没有数据,就要从Bundle中拷贝到软件的沙盒中。
-(BOOL) judgeFileExist:(NSString * )filePath
{
NSLog(@"%@", filePath);
if (![[NSFileManager defaultManager] fileExistsAtPath:filePath])
{
NSString *sourceFile =[[NSBundle mainBundle] pathForResource:@"test.db" ofType:nil];
NSLog(@"%@, %d",sourceFile, [sourceFile length]);
if ([sourceFile length] > 0)
{
NSData *sourceData = [NSData dataWithContentsOfFile:sourceFile];
NSLog(@"%d", [sourceData length]);
[[NSFileManager defaultManager] createFileAtPath:filePath contents:sourceData attributes:nil];
return YES;
}
}
else
{
return YES;
}
return NO;
}
本文介绍了一种在iOS应用中判断文件是否存在于沙盒,并根据判断结果从Bundle中读取或拷贝文件的方法。这种方法有助于确保应用程序在首次运行时能够正确地加载必要的资源文件。
2403

被折叠的 条评论
为什么被折叠?



