Ste*_*man 2 iphone cocoa-touch ios4
我创建了一个iPhone应用程序,我希望能够在应用程序中加载一批数据,但不能将所有数据存储在.h或.m文件中.
有没有办法将文本文件作为资源附加到应用程序中,然后将该文本文件加载到iPhone应用程序中的NSString中.
我希望在编译时将此文本数据文件包含在iPhone应用程序中,但是在运行时访问数据.
谢谢!
当然,这很容易.只需将.txt文件添加到项目中(将它们拖到"文件"树中).要访问它们,您需要先获取路径:
NSString *path = [[NSBundle mainBundle] pathForResource: @"filename" ofType: @"txt"];
Run Code Online (Sandbox Code Playgroud)
然后加载字符串:
NSError *error = nil;
NSString *fileData = [NSString stringWithContentsOfFile: path encoding: NSUTF8StringEncoding error: &error];
Run Code Online (Sandbox Code Playgroud)