如何编辑应用程序包中的文件?

2024-05-16

在我的应用程序中,我从存储在捆绑资源中的 CSV 文件加载数据。但是,我希望能够在用户点击“更新”按钮时以编程方式更新此文件。有没有办法以编程方式更改应用程序包中的资源?这是我用来访问该文件的代码:

    NSString *path = [[NSBundle mainBundle] pathForResource:@"query_result" ofType:@"csv"];
    NSArray *rows = [NSArray arrayWithContentsOfCSVFile:path];

首先从捆绑包加载文件,然后将其存储在文档目录中,然后您可以再次对此文件进行更改保存在文档目录中以访问更改的文件。

使用此代码从主包复制到文档。

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"];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何编辑应用程序包中的文件? 的相关文章

随机推荐