使用Objective-C以编程方式读取文本文件 [英] Read Text file Programmatically using Objective-C

查看:81
本文介绍了使用Objective-C以编程方式读取文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是iPhone编程新手

I am new to iPhone Programming

我想阅读我的Resourse文件夹中的文本文件的内容。我做了很多谷歌搜索,但未能正确完成这项任务。

I want to read the content of text file which is in my Resourse folder. i did a lot of googling but failed to get proper way for doing this task.

请建议

推荐答案

资源文件夹中的文件实际上是应用程序包的内容。首先,您需要在应用程序包中获取文件的路径。

The files in your "Resource folder" are actually the contents of your application bundle. So first you need to get the path of the file within your application bundle.

NSString* path = [[NSBundle mainBundle] pathForResource:@"filename" 
                                                 ofType:@"txt"];

然后将内容加载到 NSString 中更简单。

Then loading the content into a NSString is even easier.

NSString* content = [NSString stringWithContentsOfFile:path
                                              encoding:NSUTF8StringEncoding
                                                 error:NULL];

作为奖励,您可以拥有 文件名的不同本地化版本。 txt ,此代码将正确获取当前所选语言的文件。

As a bonus you can have different localized versions of filename.txt and this code will fetch the file of the currently selected language correctly.

这篇关于使用Objective-C以编程方式读取文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆