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

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

问题描述

我是 iPhone 编程的新手

I am new to iPhone Programming

我想阅读我的资源文件夹中的文本文件的内容.我做了很多谷歌搜索,但没有找到正确的方法来完成这项任务.

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];

作为奖励,您可以拥有 filename.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天全站免登陆