crash after reading textfile to tableview
I would like to read a textfile from NSBundle which I have saved before in Xcode to an Array, to fill the data into a UITableView.
I have a textfile with information like this "this;is;a;test" named cart.txt
Now I tried the following开发者_StackOverflow社区 code, and I can fill it into an array, but if I trie to set the rows of my tableview to the array count, the whole app crashes.
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"cart" ofType:@"txt"];
if (filePath) {
NSString *textFromFile = [[NSString alloc]initWithContentsOfFile:filePath];
lines = [textFromFile componentsSeparatedByString:@";"];
}
I can read the lines array like textfield.text = [lines objectAtIndes:0];
to a textfield. everything works.
But If I trie to get my data in a tableview, or even set the rows count to [lines count]
the app crashes.
can someone help me with that problem?
I hope so :-(
Thank you in advance.
I guess it crashes because "lines" array is already released when you send "count" message to it. Try lines = [[textFromFile componentsSeparatedByString:@";"] retain];
and release "lines" in view controllers dealloc method
精彩评论