2016-12-16 58 views
-2

我有一個框架,它需要從導入到的項目的.string文件。如何將我的應用程序的[NSBundle mainBundle]中的.string文件保存到NSLibraryDirectory,然後讓框架從NSLibraryDirectory中讀取它?從/到目錄讀/寫.string文件

謝謝

回答

0

你可以得到路徑捆綁目錄和圖書館目錄,並從包移到文件庫目錄與CopyItemAtPath

NSArray *libraryPath=NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES); 
NSString *directoryPath = [libraryPath objectAtIndex:0]; 
NSString *imagePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Templete.string"]; 

// Copy the image Folder from the package to the users filesystem 
[fileManager copyItemAtPath:imagePathFromApp toPath:directoryPath error:nil]; 
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";