繁体   English   中英

如何在应用程序捆绑包中编辑文件?

[英]How can I edit a file in app bundle?

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

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

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

使用此代码从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;
}

然后获取保存的文件进行更改...

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                     NSUserDomainMask,
                                                     YES);

NSString *fullPath = [[paths lastObject] stringByAppendingPathComponent:@"recentDownload.txt"];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM