在我的应用程序中,我从包资源中存储的CSV文件加载数据。但是,当用户点击update按钮时,我希望能够以编程方式更新此文件。有没有办法以编程方式更改应用程序包中的资源?下面是我用来访问该文件的代码:
NSString *path = [[NSBundle mainBundle] pathForResource:@"query_result" ofType:@"csv"];
NSArray *rows = [NSArray arrayWithContentsOfCSVFile:path];发布于 2014-07-23 23:43:50
首先从捆绑包中加载文件,然后将其存储在文档目录中,然后可以再次对该文件进行更改,保存到文档目录中以访问更改后的文件。
在mainbundle和文档之间使用此代码副本。
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;
}然后让保存的文件进行更改...
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *fullPath = [[paths lastObject] stringByAppendingPathComponent:@"recentDownload.txt"];https://stackoverflow.com/questions/24914769
复制相似问题