Read csv file ios

i have a problem for read a csv file. Only the last line of csv file is display.
However in my fetchedResultsController i have 2 lines
This is the code :

NSString * writeString;
NSInteger i = 0;
for (id object in [[self fetchedResultsController] fetchedObjects]) {
    NSString * object1 = [[object valueForKey:@"object1"] description];
    NSString * object2 = [[object valueForKey:@"object2"] description];
    NSString * object3 = [[object valueForKey:@"object3"] description];
    NSString * object4 = [[object valueForKey:@"object4"] description];
    writeString = [NSString stringWithFormat:@"%@, %@, %@, %@, \n", object1, object2, object3, object4];
    i++;
    NSLog(@"%@ - %i", writeString, i);

}


NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* savePath = [paths objectAtIndex:0];
savePath = [savePath stringByAppendingPathComponent:@"myfile.csv"];
[writeString writeToFile:savePath atomically:YES encoding:NSUTF8StringEncoding error:NULL];
NSLog(@"%@", savePath);

NSString *fullPath = savePath;

[self readTitleFromCSV:fullPath  AtColumn:0];

And The readTitleFromCSV method :

  • Where should NSManagedObjectContext be created?
  • Dropbox iOS SDK: WebKit Discarded an Uncaught Exception
  • SpriteKit iPhone/iPad different resolution, need same physics
  • How to hide uitabbarcontroller
  • iOS: App is not asking user's permission while installing the app. getting kCLAuthorizationStatusNotDetermined every time - Objective-c & Swift
  • Get the precise time of system bootup on iOS/OS X
  • -(void)readTitleFromCSV:(NSString*)path AtColumn:(int)column
    {
    
    NSMutableArray *titleArray=[[NSMutableArray alloc]init];
    
    NSString *fileDataString=[NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSArray *linesArray=[fileDataString componentsSeparatedByString:@"\n"];
    
    
    int k=0;
    for (id string in linesArray)
        if(k<[linesArray count]-1){
    
            NSString *lineString=[linesArray objectAtIndex:k];
            NSArray *columnArray=[lineString componentsSeparatedByString:@","];
            [titleArray addObject:[columnArray objectAtIndex:column]];
            k++;
    
        }
    
    NSLog(@"%@",titleArray);
    
    }
    

    Thank you for your help.

    Solutions Collect From Internet About “Read csv file ios”

    Try to use NSMutableString instead of NSString:

    NSMutableString *writeString = [NSMutableString string];
    

    And then in the for loop:

    [writeString appendString:[NSString stringWithFormat:@"%@, %@, %@, %@, \n", object1, object2, object3, object4]];