开发者

crash after reading textfile to tableview

开发者 https://www.devze.com 2023-03-06 05:59 出处:网络
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 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

0

精彩评论

暂无评论...
验证码 换一张
取 消