从/向目录读/写.string文件

时间:2016-12-16 08:10:21

标签: ios objective-c nsfilemanager

我有一个框架,需要从项目导入的.string文件。如何将.string文件从我的应用程序的[NSBundle mainBundle]保存到NSLibraryDirectory,然后让框架从NSLibraryDirectory读取它?

谢谢

2 个答案:

答案 0 :(得分:0)

您可以获取捆绑目录和库目录的路径,并使用(name)=> s"Your name is $name"

将文件从捆绑包移动到库目录
CopyItemAtPath

答案 1 :(得分:0)

感谢@iOS_Developer写作部分。

NSString *resourcepath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"filename.strings"];

NSString *filePath = [kSXBFileManLibraryDirectoryPath stringByAppendingPathComponent:@"filename.strings"];

写:

NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
if ([fileManager copyItemAtPath: resourcepath toPath:destination error:&error]){
    NSLog(@"Copy Success");
}
else{
    NSLog(@"Copy error: %@", error);
}

读:

NSString *newPath = [kLibraryDirectoryPath stringByAppendingPathComponent:@"filename.strings"];
NSDictionary *contents = [NSDictionary dictionaryWithContentsOfFile:newPath];
NSLog(@"contents:%@", [contents objectForKey:@"login.background"]);

其中;

#define kLibraryDirectoryPath [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject]

filename.strings

"login.background"          = "FE8100";
"login.textField"           = "FE8100";
"login.button"              = "1B0D62";