PList 鸡/蛋场景

发布于 2024-09-02 02:55:17 字数 477 浏览 10 评论 0原文

我想读取/写入cache.plist

如果我想读取存储在资源文件夹中的现有预制plist 文件,我可以:

path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathWithComponent@"cache.plist"];
NSMutableDictionary *root = ...

但是我希望从iPhone 读取它。

不能,Resources 文件夹是只读的。

所以我需要使用:

NSDocumentDirectory, NSUserDomain,YES

那么如何将我的 plist 文件预安装到文档目录位置?

因此,这意味着我不必在启动时使用乱七八糟的代码复制 plist 文件。 (除非这是唯一的方法)。

I want to read/write to cache.plist

If I want to read an existing premade plist file stored in the resources folder I can go:

path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathWithComponent@"cache.plist"];
NSMutableDictionary *root = ...

But then I wish to read it from the iPhone.

Can't, the Resources folder is only readable.

So I need to use:

NSDocumentDirectory, NSUserDomain,YES

So how can I have my plist file preinstalled to the Document Directory location?

Thus meaning I don't have to mess around with untidy code copying the plist file over at startup. (Unless that's the only way).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

待"谢繁草 2024-09-09 02:55:17

我知道这并不是您真正想要的,但据我所知,将文档放入“文档”文件夹的唯一方法是将其实际复制到那里......但仅限于第一次启动时。我正在为 sqlite 数据库做类似的事情。代码如下,它可以工作,但请注意,它可以进行一些清理:

// Creates a writable copy of the bundled default database in the application Documents directory.
- (void)createEditableCopyOfDatabaseIfNeeded {
    // First, test for existence.
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"WordsDatabase.sqlite3"];
    createdDatabaseOk = [fileManager fileExistsAtPath:writableDBPath];
    if (createdDatabaseOk) return;
    // The writable database does not exist, so copy the default to the appropriate location.
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"WordsDatabase.sqlite3"];
    createdDatabaseOk = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
}

只需调用您的 AppDelegate - 真的不太乱吗?

I know this isn't really what you're after, but as far as I know the only way to get the document into the Documents folder IS to actually copy it there...but only on the first startup. I'm going something similar for a sqlite database. Code is below, it works but please note it could do with a little bit of cleaning up:

// Creates a writable copy of the bundled default database in the application Documents directory.
- (void)createEditableCopyOfDatabaseIfNeeded {
    // First, test for existence.
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"WordsDatabase.sqlite3"];
    createdDatabaseOk = [fileManager fileExistsAtPath:writableDBPath];
    if (createdDatabaseOk) return;
    // The writable database does not exist, so copy the default to the appropriate location.
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"WordsDatabase.sqlite3"];
    createdDatabaseOk = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
}

Just call in your AppDelegate - not too messy really?

会傲 2024-09-09 02:55:17

简单的。首先查看它是否在文档目录中。如果不是,请在应用程序的资源文件夹中找到它([[NSBundle mainBundle] pathForResource...]),然后使用 < 将其复制到文档目录中a href="http://developer.apple.com/iphone/library/documentation/cocoa/Reference/Foundation/Classes/NSFileManager_Class/Reference/Reference.html#//apple_ref/doc/uid/20000305-SW14" rel= “nofollow noreferrer”>[[NSFileManager defaultManager] copyItemAtPath:...]。然后使用文档目录中的新副本而不受惩罚。

Easy. Look first to see if it's in the documents directory. If it's not, find it inside your app's Resources folder ([[NSBundle mainBundle] pathForResource...]), then copy it into the documents directory using [[NSFileManager defaultManager] copyItemAtPath:...]. Then use the fresh copy in the documents directory with impunity.

您的好友蓝忘机已上羡 2024-09-09 02:55:17

最终产品

NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"Cache.plist"];


NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *giveCachePath = [documentsDirectory stringByAppendingPathComponent:@"Cache.plist"];


BOOL fileExists = [fileManager fileExistsAtPath:giveCachePath];

if (fileExists) {
    NSLog(@"file Exists");
}
else {
    NSLog(@"Copying the file over");
    fileExists = [fileManager copyItemAtPath:finalPath toPath:giveCachePath error:&error];
}

NSLog(@"Confirming Copy:");

BOOL filecopied = [fileManager fileExistsAtPath:giveCachePath];

if (filecopied) {
    NSLog(@"Give Cache Plist File ready.");
}
else {
    NSLog(@"Cache plist not working.");
}

The final product

NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"Cache.plist"];


NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *giveCachePath = [documentsDirectory stringByAppendingPathComponent:@"Cache.plist"];


BOOL fileExists = [fileManager fileExistsAtPath:giveCachePath];

if (fileExists) {
    NSLog(@"file Exists");
}
else {
    NSLog(@"Copying the file over");
    fileExists = [fileManager copyItemAtPath:finalPath toPath:giveCachePath error:&error];
}

NSLog(@"Confirming Copy:");

BOOL filecopied = [fileManager fileExistsAtPath:giveCachePath];

if (filecopied) {
    NSLog(@"Give Cache Plist File ready.");
}
else {
    NSLog(@"Cache plist not working.");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文