将文本文件读取到表视图后崩溃
我想从 NSBundle 读取一个文本文件,我之前在 Xcode 中将其保存到数组中,以将数据填充到 UITableView 中。
我有一个文本文件,其中包含类似“this;is;a;test”的信息,名为 cart.txt
现在我尝试了以下代码,我可以将其填充到一个数组中,但是如果我尝试将表视图的行设置为数组计数,整个应用程序崩溃。
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"cart" ofType:@"txt"];
if (filePath) {
NSString *textFromFile = [[NSString alloc]initWithContentsOfFile:filePath];
lines = [textFromFile componentsSeparatedByString:@";"];
}
我可以将像 textfield.text = [lines objectAtIndes:0];
这样的行数组读取到文本字段。一切正常。
但是,如果我尝试在表视图中获取数据,或者甚至将行数设置为[行数]
,应用程序就会崩溃。
有人可以帮我解决这个问题吗?
我希望如此:-(
提前谢谢您。
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 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜它会崩溃,因为当您向它发送“count”消息时,“lines”数组已经被释放。尝试
lines = [[textFromFile ComponentsSeparatedByString:@";"] keep];
并在视图控制器 dealloc 方法中释放“lines”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