How to break a new line into NSString in ObjectiveC

For my new project. I uploaded the contents of the csv file as NSString. First, I need to split this into a new line and then split each line with a comma. How can I loop all this? Could you help me?

CSV Content

"^ GSPC", 1403.36, "4/27/2012", "4:32 pm", + 3.38, 1400.19.1406.64,1397.31,574422720 "^ IXIC", 3069.20, "4/27/2012", "5: 30 PM ", + 18.59,3060.34,3076.44,3043.30,0

ViewController.m

NSString* pathToFile = [[NSBundle mainBundle] pathForResource: @"quotes" ofType: @"csv"]; NSString *fileString = [NSString stringWithContentsOfFile:pathToFile encoding:NSUTF8StringEncoding error:nil]; if (!fileString) { NSLog(@"Error reading file."); } NSArray *array = [fileString componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]]; for(int i=0; i<[array count]; i++){ // } 
+8
ios objective-c iphone xcode nsstring
source share
1 answer

You might want to explore NSMutableArray .

 // grab your file NSMutableArray *data = [[fileString componentsSeparatedByCharactersInSet: [NSCharacterSet newlineCharacterSet]] mutableCopy]; for (int i = 0; i < [data count]; i++) { [data replaceObjectAtIndex: i withObject: [[data objectAtIndex: i] componentsSeparatedByString: @","]]; } 

In addition, Dave DeLong has an appropriate library to solve this problem: https://github.com/davedelong/CHCSVParser

+13
source share

All Articles