[英]How can I edit a file in app bundle?
In my app, I load data from a CSV file stored in the bundle resources. 在我的应用程序中,我从捆绑资源中存储的CSV文件加载数据。 However, I'd like to be able to update this file programmatically when the user taps the Update button.
但是,当用户点击“更新”按钮时,我希望能够以编程方式更新此文件。 Is there a way to change a resource in the app bundle programatically?
是否可以通过编程方式更改应用程序捆绑包中的资源? Here is the code I use to access the file:
这是我用来访问文件的代码:
NSString *path = [[NSBundle mainBundle] pathForResource:@"query_result" ofType:@"csv"];
NSArray *rows = [NSArray arrayWithContentsOfCSVFile:path];
First of all load file from bundle and after that just store in Document directory then you can make change on this file again save on document directory to access changed file. 首先从包中加载文件,然后将其存储在文档目录中,然后您可以对该文件进行更改,然后再次保存在文档目录中以访问更改的文件。
Use this code copy from mainbundle to Document. 使用此代码从mainbundle复制到Document。
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSString *dataPath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"tessdata"];
NSLog(@"Datapath is %@", dataPath);
// If the expected store doesn't exist, copy the default store.
if ([fileManager fileExistsAtPath:dataPath] == NO)
{
NSString *tessdataPath = [[NSBundle mainBundle] pathForResource:@"eng" ofType:@"traineddata"];
[fileManager copyItemAtPath:tessdataPath toPath:dataPath error:&error];
}
-(NSString*) applicationDocumentsDirectory{
// Get the documents directory
NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = [dirPaths objectAtIndex:0];
return docsDir;
}
And then get the Saved file to change... 然后获取保存的文件进行更改...
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *fullPath = [[paths lastObject] stringByAppendingPathComponent:@"recentDownload.txt"];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.