在iPhone项目中加载数据文件 [英] Loading data files in iPhone project
问题描述
如何在iPhone项目中读取数据文件?例如,假设我有一个名为level.dat的静态文件,结构如下:
障碍:10
时间:100
障碍1:10,20
。 ..
How does one read a data file in an iPhone project? For example, lets say I have a static file called "level.dat" that is structured as follows: obstacles: 10 time: 100 obstacle1: 10,20 ...
我想读取文件的内容到NSString然后做解析。如何将文件的内容读入字符串?另外,在项目中level.dat文件应驻留在哪里?
I would like to read the contents of the file into a NSString then do the parsing. How do I read the contents of a file into a string? Also, where in the project should the "level.dat" file reside? Should it be under "Resources" or just in the main directory?
推荐答案
方案
查看此答案:如何在iPhone上fopen() ?,显示如何访问您的捆绑包中的资源。一旦你有了路径,只需使用[NSString stringWithContentsOfFile:encoding:error:]。
See this answer: How to fopen() on the iPhone? which shows how to get access to resources in your bundle. Once you have the path, just use [NSString stringWithContentsOfFile:encoding:error:].
NSString *path = [[NSBundle mainBundle] pathForResource: @"level" ofType: @"dat"]
NSError *error = nil;
NSString *data = [NSString stringWithContentsOfFile: path
encoding: NSUTF8StringEncoding
error: &error];
这篇关于在iPhone项目中加载数据文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!