使用[NSBundle mainBundle]写文件失败

编程入门 行业动态 更新时间:2024-10-24 12:24:49
本文介绍了使用[NSBundle mainBundle]写文件失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图从一个文件中获取内容,并将其写入另一个文件。我读得很好,但我不能写入另一个文件。

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.

我有一个数据库的单词。我想根据字母数量将单词分成不同的文件。所有四个字母的单词进入一个文件,等等。我在我的资源中添加了一个名为4letter的txt文件,以下是我的代码:

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. I added a txt file called "4letter" into my resources and the following is my code:

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. Use a path into the Documents folder instead.

更多推荐

使用[NSBundle mainBundle]写文件失败

本文发布于:2023-07-16 07:26:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1119336.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   NSBundle   mainBundle

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!