When I run my app in Xcode using the simulator it runs just file until I add in the first three lines involving the text file. It gives me a message saying terminate called after throwing an instance of 'NSException'.
I don't know what that means or why it only happens when I try to read in the text file and display it in the textView.
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"textfiles/brain_01" ofType:@"txt"];//establish file path for text file
NS开发者_开发问答String *textFile = [[NSString alloc] stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error: nil];
textView.text = textFile;
[super viewDidLoad];
fullButton.hidden = YES;
viewLabel.hidden = YES;
}
NSException
means your code has a bug in it. An exception is an error. In your case, it's probably because
NSString *textFile = [[NSString alloc] stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error: nil];
should be
NSString *textFile = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error: nil];
stringWithContentsOfFile
is a class method used to make NSString
-- only use alloc
with init...
methods.
精彩评论