0

I have implemented the module for reading the csv file

The project structure is as follows:

Project
 --test_de.csv
 --Folder
   --Controller.h
   --Controller.m

but the result shows no response. No words are added:

2014-06-19 15:32:16.817 marker[1748:60b] /var/mobile/Applications/E2B95450-429D-4777-97BE-0209522EFDEF/Documents/test_de.csv
2014-06-19 15:32:16.824 marker[1748:60b] (
)

The below is my code

    NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* fullPath = [paths objectAtIndex:0];
    fullPath = [fullPath stringByAppendingPathComponent:@"test_de.csv"];
    NSLog(@"%@", fullPath);

 NSMutableArray *titleArray=[[NSMutableArray alloc]init];

    NSString *fileDataString=[NSString stringWithContentsOfFile:fullPath 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];
                NSLog(@"%@",lineString);
            NSArray *columnArray=[lineString componentsSeparatedByString:@"\n"];
            [titleArray addObject:[columnArray objectAtIndex:0]];
            k++;

        }

    NSLog(@"%@",titleArray);
Rachel Gallen
  • 27,943
  • 21
  • 72
  • 81
Jeff Bootsholz
  • 2,971
  • 15
  • 70
  • 141

2 Answers2

1

Tip: use JSON!

You are better off using a JSON format instead CVS for your resource - regardless whether this resource comes from a backend at runtime or has been embedded as an application resource at build time.

You can use the iOS system JSON decoder to parse the file into an object representation. This saves you from the many hassles to implement your own CVS parser.

Also, it should be a no-brainer to convert any input provided in CVS into JSON before you actually provide it as a resource for your application.

How to read an app resource?

Well, first ensure your resource file is included the project and has been assigned a target (see "Target Membership" in the File Inspector pane).You can also check this in the "Copy Bundle Resources" section in the "Build Phases" tab in the target editor when you select the associated target. Your file should be listed there.

Assuming, you put the given file as an application resource, you should use the Bundle class to read that resource. First get its location as a URL:

(Note: I'm only showing this in Swift and leave the Objective-C version as an exercise)

Swift:

var fileUrl = Bundle.main.url(forResource: "test_de", withExtension: "json")

This also assumes, the resource is in the root folder of the app resource folder, which happens to be the default case when you flag a file as a resource. Note, that the "app resource folder" is "opaque" to you, that is, actually you do not need to know where it is - the Bundle handles all that for you.

For Objective-C you may read here: NSBundle

Now, that you have the URL, read the file and get a Data object:

Swift:

let data = try Data(contentsOf: fileUrl)

For Objective-C you may read here: NSData

Obtain an Object Representation for your Resource

Once you have the file as data (NSData or Data), and assuming its content is a JSON Array you can get an object representation as follows:

Objective-C:

NSError *error = nil;
id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error];

if ([jsonObject isKindOfClass:[NSArray class]]) {
    ...
}
else {
    NSLog(@"did not find a JSON Array");
    ...
}

Swift:

Doing it swiftly would require you to define a type (struct) which represents an element in the JSON Array. Usually, this is easy to accomplish:

struct MyElement: Codable {
    let field1: String 
    let field2: String 
    let field3: String   
}

Then, use the Swift JSON Decoder to get an array of MyElements:

let elements = try JSONDecoder.decode([MyElement].self, from: data)
CouchDeveloper
  • 18,174
  • 3
  • 45
  • 67
0

According to SoulWithMobileTechnology this is how to do it

 [self readTitleFromCSV:csvPath  AtColumn:0];

And our method looks like following:

-(void)readTitleFromCSV:(NSString*)path AtColumn:(int)column
{


    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);

}
Rachel Gallen
  • 27,943
  • 21
  • 72
  • 81