【发布时间】:2010-07-02 18:31:38
【问题描述】:
我有以下代码,它从字符串数组中获取一个字符串并将该字符串分成两部分。
NSString *temp = [tableViewData objectAtIndex:indexPath.row];
NSArray *tempArray = [temp componentsSeparatedByString: @"_"];
cell.textLabel.text = [tempArray objectAtIndex:1];
添加的字符串如下
newTitle = [NSString stringWithFormat:@"%d_%@",[Daily_QuoteViewController counter],title];
[plistArray addObject:newTitle];
[plistArray writeToFile:filepath atomically: YES];
这是添加一个索引和一个字符串。
我试图在第一个代码部分中拆分该字符串。但是尝试访问索引 1 处的字符串的第二部分会出现越界错误。
由于未捕获的异常“NSRangeException”而终止应用程序,原因:“*** -[NSArray objectAtIndex:]: index 1 beyond bounds [0 .. 0]”
为了使用字符串的两个部分,最好的分割字符串的方法是什么
拆分firstPart_secondPart
提前致谢。
【问题讨论】:
-
你的代码没问题。我假设 temp 没有像你期望的那样保存一个字符串 - 你能检查一下吗?
-
temp 是一个字符串。我可以看到它添加到数组中,它将数组读取到表视图中,但是当尝试拆分并使用它时,我得到了一个超出范围的错误。
-
它为字符串的后半部分找到 0 索引,但没有找到索引 1。请指教。
标签: iphone objective-c nsstring