iPad Text File Importing
I'm trying to import a text file into my iPad application using the method initWithContentsOfFile:encoding:error:, but when I try to get the filepath to my text file, I am running into some troubles. It seems that my text file is not being found. The following call returns nil (the text file is called legal.txt)
NSString *filePath = [[NSBundle ma开发者_开发技巧inBundle] pathForResource:@"legal"
ofType:@"txt"];
Obviously, after this returns nil, my call...
NSString *fileContents = [[NSString alloc] initWithContentsOfFile:filePath
encoding:NSUTF8StringEncoding
error:@"error"];
also returns nil.
I am quite new at this so I might be doing something obviously incorrect, but looked all over SO and elsewhere and couldn't find anyone with the same problem as me.
Call pathForResource:ofType:inDirectory: by passing the directory as well
Look in your app target's Copy Resources build phase and make sure legal.txt
is making it into your app bundle. You can also look inside the compiled app package in your build folder (~/Library/Developer/Xcode/DerivedData/AppName-.../
by default in Xcode 4).
精彩评论