我在XCode上创建了一个plist,它将具有一些无法手动插入的值。因此,我想在开发时以编程方式添加此值。但是似乎我只能读取plist,但我无法保存App捆绑包中的plist,这在运行时很有意义。.当我分发我的应用程序时,我希望每个人都拥有这个plist文件,这就是为什么我不保存在文档或缓存上。我怎样才能实现自己想要的?
最佳答案
通过http://www.karelia.com/cocoa_legacy/Foundation_Categories/NSFileManager__Get_.m(下面粘贴),您可以使用在那里找到的-(NSString *) pathFromUserLibraryPath:(NSString *)inSubPath
方法在用户的个人库中构建路径。
例如,NSString *editedPlist = [self pathFromUserLibraryPath:@"my.plist"];
为您提供用户库中经过修改的plist的名称(即使该plist尚不存在)。
根据您拥有哪种类型的plist来读取/写入它,但是您可以使用以下方法将其读入字典中:
NSMutableDictionary *thePlist= [[NSMutableDictionary alloc] initWithContentsOfFile:editedPlist ];
如果您无法阅读(例如,通过
[thePlist count] == 0
轻易检测到),则可以调用同一initWithContentsOfFile:
初始化程序,并使用捆绑包中模板的路径,但随后将plist写出到editedPlist
路径中,以便它出现在用户目录。这是我上面引用的实用程序方法:
/*
NSFileManager: Get the path within the user's Library directory
Original Source: <http://cocoa.karelia.com/Foundation_Categories/NSFileManager__Get_.m>
(See copyright notice at <http://cocoa.karelia.com>)
*/
/*" Return the path in the user library path of the given sub-path. In other words, if given inSubPath is "foo", the path returned will be /Users/myUser/Library/foo
"*/
- (NSString *) pathFromUserLibraryPath:(NSString *)inSubPath
{
NSArray *domains = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask,YES);
NSString *baseDir= [domains objectAtIndex:0];
NSString *result = [baseDir stringByAppendingPathComponent:inSubPath];
return result;
}
关于ios - 在开发时在App捆绑中以编程方式编辑plist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23721505/