文件路径不断变化

时间:2015-04-02 22:56:17

标签: ios objective-c nsfilemanager

NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentDir = [documentPaths objectAtIndex:0];

self.databasePath = [documentDir stringByAppendingPathComponent:kAppDatabase];


-(void) copyDataBaseIfNotExist

{
    BOOL success;

NSFileManager *fileManager = [NSFileManager defaultManager];

success = [fileManager fileExistsAtPath:self.databasePath];

if(success) return;

//This block will run for the first time only

NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:kAppDatabase];

// Make a copy of the file in the Documents folder
[fileManager copyItemAtPath:databasePathFromApp toPath:self.databasePath error:nil];
db = [FMDatabase databaseWithPath:self.databasePath];

[self insertDummyData];

NSLog(@"%@ Database path %s ",self.databasePath,__PRETTY_FUNCTION__);

}

上述代码将文件复制到文档文件夹中。现在它第一次保存了。文件路径是:

811E4F6C-B282-402A-859B-40FA6DBD2B3C/data/Containers/Data/Application/E6F63F62-30F6-4580-BF4D-C16DE2B33292/Documents/AppDatabase.db

第二次从以下地址访问:

811E4F6C-B282-402A-859B-40FA6DBD2B3C/data/Containers/Data/Application/48771A22-0616-4162-9DF9-8C5CC4AF6D39/Documents/AppDatabase.db

为什么每次都会改变,不应该一样?

1 个答案:

答案 0 :(得分:1)

如果您在iOS8中运行您的应用,它将继续改变路径。 在这里结账:http://pinkstone.co.uk/where-is-the-documents-directory-for-the-ios-8-simulator/