2

现在我的应用程序正在将数据库放在 ~/Library 文件夹中,但我希望它以更有条理的方式放置它 ~/Library/App Name 但我不知道如何使用这段代码来做到这一点。

(NSPersistentStoreCoordinator *) persistentStoreCoordinator {

if (persistentStoreCoordinator != nil) {
    return persistentStoreCoordinator;
}

NSFileManager *fileManager;
NSString *applicationSupportFolder = nil;
NSString *dataFilePath;
NSURL *url;
NSError *error;

fileManager = [NSFileManager defaultManager];
applicationSupportFolder = [self applicationSupportFolder];
if ( ![fileManager fileExistsAtPath:applicationSupportFolder isDirectory:NULL] ) {
    [fileManager createDirectoryAtPath:applicationSupportFolder attributes:nil];
}


dataFilePath = [applicationSupportFolder stringByAppendingPathComponent: @"sample.dat"];


if( NO == [[ NSFileManager defaultManager ] fileExistsAtPath: dataFilePath ] )
{

    [[ NSFileManager defaultManager ] copyPath: [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: @"sample.dat"]
        toPath: dataFilePath handler: NULL ];

}

url = [NSURL fileURLWithPath:dataFilePath];
if( url )
{           
    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];


    if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url options:nil error:&error]){

        [[NSApplication sharedApplication] presentError:error];
    }    
}
return persistentStoreCoordinator;
}

此代码有效,它只是将数据库放置在 IMO 非常糟糕的位置。我是 Obj-C 的新手,刚刚从另一个开发人员那里继承了这个代码集。

更新:

在下面添加最终的工作代码。

(NSPersistentStoreCoordinator *) persistentStoreCoordinator {

if (persistentStoreCoordinator != nil) {
    return persistentStoreCoordinator;
}

NSFileManager *fileManager;
NSString *applicationSupportFolder = nil;
NSString *dataFilePath;
NSURL *url;
NSError *error;

fileManager = [NSFileManager defaultManager];
applicationSupportFolder = [self applicationSupportFolder];
NSString *aappSupportFolder = [applicationSupportFolder stringByAppendingPathComponent: @"APP"];

if ( ![fileManager fileExistsAtPath:aappSupportFolder isDirectory:NULL] ) {
    [fileManager createDirectoryAtPath:aappSupportFolder attributes:nil];
}

dataFilePath = [aappSupportFolder stringByAppendingPathComponent: @"sample.dat"];


if( NO == [[ NSFileManager defaultManager ] fileExistsAtPath: dataFilePath ] )
{

    [[ NSFileManager defaultManager ] copyPath: [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: @"sample.dat"]
        toPath: dataFilePath handler: NULL ];

}

url = [NSURL fileURLWithPath:dataFilePath];
if( url )
{           
    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];


    if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url options:nil error:&error]){

        [[NSApplication sharedApplication] presentError:error];
    }    
}
return persistentStoreCoordinator;
}
4

1 回答 1

1

嗯,这很简单,您只需在机器的库文件夹中创建文件夹名称 APP ,然后您必须将您的应用程序移动到同一路径中。请在下面尝试:-

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *pathString= [paths objectAtIndex:0];
 NSError *err=nil;
//below is your application previous path which we will move to new path
NSString  *dataFilePath = [pathString stringByAppendingPathComponent: @"sample.dat"];
NSString *applicationSupportFolder = [pathString stringByAppendingPathComponent: @"App"];
//Here we have created directory folder as App inside Library below
if ( ![fileManager fileExistsAtPath:applicationSupportFolder isDirectory:NULL] ) {
    [fileManager createDirectoryAtPath:applicationSupportFolder attributes:nil];
}

   if ([fm moveItemAtPath:dataFilePath toPath:applicationSupportFolder error:&err])
        {
            NSLog(@"success");
        }
 else
        {
            NSLog(@"%@",[err localizedDescription]);
        }
于 2013-10-13T17:23:57.487 回答