简体   繁体   English

使用 [NSBundle mainBundle] 写入文件失败

[英]File write with [NSBundle mainBundle] fails

I am trying to take content from one file and write it into another.我正在尝试从一个文件中获取内容并将其写入另一个文件。 I am reading fine, but I am not able to write it into another file.我读得很好,但我无法将它写入另一个文件。

I have a database of words.我有一个单词数据库。 I want to separate the words into different files based on the number of letters.我想根据字母的数量将单词分成不同的文件。 All four letter words go into one file, and so on.将所有四个字母的单词 go 放入一个文件中,以此类推。 I added a txt file called "4letter" into my resources and the following is my code:我在资源中添加了一个名为“4letter”的 txt 文件,以下是我的代码:

NSError *error;

//READ
NSString *dbFile = [[NSBundle mainBundle] pathForResource:@"words" ofType:@"txt"];
NSString *test = [NSString stringWithContentsOfFile:dbFile encoding:NSUTF8StringEncoding error:&error];

//convert from string to array
NSArray *lines = [test componentsSeparatedByString:@"\n"]; 

NSFileHandle *logFile = nil;
logFile = [NSFileHandle fileHandleForWritingAtPath:[[NSBundle mainBundle] pathForResource:@"4letter" ofType:@"txt"]];

//Test if write works
for (int i=0; i<5; i++) 
{
    NSString *randomAnagram = [[lines objectAtIndex:i] lowercaseString];
    [logFile writeData: [randomAnagram dataUsingEncoding: NSNEXTSTEPStringEncoding]];
}

In iOS, you can't write into a file in your app's bundle -- the entire bundle is read-only.在 iOS 中,您无法写入应用程序包中的文件——整个包是只读的。 Use a path into the Documents folder instead.请改用 Documents 文件夹的路径。

See special File System Programming Guide for better understnading.请参阅特殊的文件系统编程指南以获得更好的理解。
In iOS, you can't write into a file in your app's bundle -- the entire bundle is read-only.在 iOS 中,您无法写入应用程序包中的文件——整个包是只读的。

Consider reading iOS Data Storage Guidelines to better understand the purpose of directories below, in context of iCloud backup.考虑阅读iOS 数据存储指南,以更好地了解以下目录在 iCloud 备份上下文中的用途。

<Application_Home>/AppName.app <Application_Home>/AppName.app

This is the bundle directory containing the app itself.这是包含应用程序本身的捆绑目录。 Do not write anything to this directory.不要向此目录写入任何内容。 To prevent tampering, the bundle directory is signed at installation time.为防止篡改,捆绑目录在安装时进行签名。 Writing to this directory changes the signature and prevents your app from launching again.写入此目录会更改签名并阻止您的应用再次启动。


<Application_Home>/Documents/ <Application_Home>/文件/

Use this directory to store critical user documents and app data files.使用此目录来存储重要的用户文档和应用程序数据文件。 Critical data is any data that cannot be recreated by your app, such as user-generated content.关键数据是您的应用程序无法重新创建的任何数据,例如用户生成的内容。 The contents of this directory can be made available to the user through file sharing.该目录的内容可以通过文件共享提供给用户。 The contents of this directory are backed up by iTunes.此目录的内容由 iTunes 备份。


<Application_Home>/Library/ <Application_Home>/库/

This directory is the top-level directory for files that are not user data files.此目录是非用户数据文件的顶级目录。 You typically put files in one of several standard subdirectories but you can also create custom subdirectories for files you want backed up but not exposed to the user.您通常将文件放在几个标准子目录之一中,但您也可以为要备份但不向用户公开的文件创建自定义子目录。 You should not use this directory for user data files.您不应将此目录用于用户数据文件。 The contents of this directory (with the exception of the Caches subdirectory) are backed up by iTunes.此目录的内容(Caches 子目录除外)由 iTunes 备份。 For additional information about the Library directory, see “The Library Directory Stores App-Specific Files.”有关库目录的更多信息,请参阅“库目录存储 App 特定文件”。

See full list (tmp/, Documents/Inbox) in iOS Standard Directories: Where Files Reside请参阅iOS 标准目录中的完整列表(tmp/、文档/收件箱):文件所在的位置

UPDATE更新
I use NSFileManager method URLForDirectory:inDomain:appropriateForURL:create:error:我使用 NSFileManager 方法URLForDirectory:inDomain:appropriateForURL:create:error:

Like Caleb said, you can't write to your app's directory, but you can write to your app's Documents folder.正如 Caleb 所说,您不能写入应用程序的目录,但可以写入应用程序的 Documents 文件夹。 You can get it like this:你可以像这样得到它:

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

Your app's bundle is read-only.您的应用程序包是只读的。 There is two ways I could see:我可以看到两种方式:

1) Write in documents folder: 1)写入文件文件夹:

NSArray *pathList = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path =  [myPathList  objectAtIndex:0];

2) Use sqlite database. 2) 使用 sqlite 数据库。 This is the same as 1 (you must save db in documents anyway), but you're using sqlite database.这与 1 相同(无论如何您必须将 db 保存在文档中),但您使用的是 sqlite 数据库。 I think this is better than a lot of txt and plist files: here's a tutorial on the topic .我认为这比很多 txt 和 plist 文件要好: 这里有一个关于该主题的教程

I use the following code:我使用以下代码:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"set.txt"];

NSString *data=@"Kostas";

[data writeToFile:appFile atomically:YES];   

NSString *myData = [NSString stringWithContentsOfFile:appFile];   

NSLog(@"Data : %@ ",myData);

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

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