在我的应用中,我从存储在捆绑资源中的CSV文件加载数据。但是,当用户点击“更新”按钮时,我希望能够以编程方式更新此文件。有没有办法以编程方式更改应用程序包中的资源?这是我用来访问文件的代码:
NSString *path = [[NSBundle mainBundle] pathForResource:@"query_result" ofType:@"csv"];
NSArray *rows = [NSArray arrayWithContentsOfCSVFile:path];
答案 0 :(得分:1)
首先从bundle加载文件,然后只存储在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;
}
然后让Saved文件更改......
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *fullPath = [[paths lastObject] stringByAppendingPathComponent:@"recentDownload.txt"];