简体   繁体   中英

Splitting an NSString

I have the following code which fetches a string from an array of strings and splits the string into 2 parts.

NSString *temp = [tableViewData objectAtIndex:indexPath.row];
NSArray *tempArray = [temp componentsSeparatedByString: @"_"];
cell.textLabel.text = [tempArray objectAtIndex:1];

and the String which is added is as follows

newTitle = [NSString stringWithFormat:@"%d_%@",[Daily_QuoteViewController counter],title];
[plistArray addObject:newTitle];
[plistArray writeToFile:filepath atomically: YES];

Which is adding an index and a string.

I am trying to then split that string in the first code section. but trying to access the second part of the string at index 1 gives an out of bounds error.

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSArray objectAtIndex:]: index 1 beyond bounds [0 .. 0]'

What is the best way to split the string in order to use both parts of the string

split firstPart_secondPart

Thanks in advance.

This seems fine to me. Can you print out NSString temp? NSLog(@"%@", temp);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM