Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The depth of your processing will determine what sort of data structure is required for this task. This is the method I would use:</p> <p><strong>1:</strong> Read the <code>.csv</code> file into one giant <code>NSString</code> object:</p> <pre><code>NSString *file = [[NSString alloc] initWithContentsOfFile:yourCSVHere]; </code></pre> <p><strong>2:</strong> Get the individual lines:</p> <pre><code>NSArray *allLines = [file componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]]; </code></pre> <p><strong>3:</strong> For each line, get the individual components:</p> <pre><code>for (NSString* line in allLines) { NSArray *elements = [line componentsSeparatedByString:@","]; // Elements now contains all the data from 1 csv line // Use data from line (see step 4) } </code></pre> <p><strong>4:</strong> This is where it's up to you. My first thought would be to create a class to store all your data. For example:</p> <pre><code>@interface Record : NSObject //... @property (nonatomic, copy) NSString *name @property (nonatomic, copy) NSString *owner // ... etc @end </code></pre> <p><strong>4a:</strong> Then, back in step 3 create a <code>Record</code> object for each line and then put all the Record objects into a separate <code>NSArray</code> (something with larger scope!).</p> <p><strong>5:</strong> Use your <code>NSArray</code> that contains all your <code>Record</code> objects as the data source for your <code>UITableView</code>.</p> <p>The implementation of Steps 4 and 5 are up to you. That's probably how I would do it though for a medium sized <code>.csv</code> file.</p> <p><strong>EDIT:</strong> Here's how to generate the <code>Records</code>.</p> <pre><code>// NSMutableArray *someArrayWithLargerScope = [[NSMutableArray alloc] init]; // NSString *file = [[NSString alloc] initWithContentsOfFile:yourCSVHere]; NSArray *allLines = [file componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]; for (NSString* line in allLines) { NSArray *elements = [line componentsSeparatedByString@","]; Record *rec = [[Record alloc] init]; rec.name = [elements objectAtIndex:0]; rec.owner = [elements objectAtIndex:1]; // And so on for each value in the line. // Note your indexes (0, 1, ...) will be determined by the // order of the values in the .csv file. // ... // You'll need one `Record` property for each value. // Store the result somewhere [someArrayWithLargerScope addObject:rec]; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload