I want to incorporate a text file into the "Build" of my program for the iPhone.开发者_如何学Go Other then copying and pasting into the code, how can I do this? I have many data files that I would like to include in the build.
You can use the NSString
method stringWithContentsOfFile:encoding:error:
to read the contents of any file in your project into an NSString
.
Include the file in your project (no copy and paste required). Simply right click on the list of files, select Add
, select Existing files
, and navigate to the file to include.
Then, in the code where you want to load the file, use
NSString *txtFilePath = [[NSBundle mainBundle] pathForResource:@"myTxtFile" ofType:@"txt"];
NSString *txtFileContents = [NSString stringWithContentsOfFile:txtFilePath encoding:NSASCIIStringEncoding error:&error];
However, if you want to have more structure, then you should use a plist
to store an NSDictionary
.
You can add a .txt file to your application bundle. Please explain more what actually you want to do?
You can use plists to provide prepared data lists in your app. You can read them in with the NSArray function arrayWithContentsOfFile:
or the NSDictionary function dictionaryWithContentsOfFile:
.
精彩评论