Creating an array from a CSV file using Objective-C

I am new to coding, so please excuse me if this seems like a simple question.

I am trying to plot coordinates on a map.

I want to read a CSV file and pass the information into two separate arrays.

The first array will be NSArray * towerInfo (containing latitude, longitude and tower name)

the second NSArray * region (containing the name and region of the tower) with the same reference index as the first array.

Essentially, I think I need:

1) read the file on the line .....

2) divide the string into a temporary array dividing on each / n / r ......

3) scroll through the temp array and each time create a Tower and Region object before adding this information to the two main storage arrays.

Is this the right process, and if so, is there anyone who can post some sample code, as I am really trying to figure it out.

Thanks to everyone in advance.

Chris.

I edited this to show an example of my code. I'm having a problem getting warnings saying

1) the "local declaration 'dataStr' hides the instance variable. 2) the" local declaration "array" hides the instance variable.

I think I understand what this means, but I don’t know how to get around this. The program compiles and runs, but the log tells me that "the array is null."

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize dataStr;
@synthesize array;

-(IBAction)convert {
//calls the following program when the onscreen 'convert' button is pressed.

    NSString *dataStr = [NSString stringWithContentsOfFile:@"Towers.csv" encoding:NSUTF8StringEncoding error:nil];
    //specifies the csv file to read - stored in project root directory - and encodes specifies that the format of the file is NSUTF8. Choses not to return an error message if the reading fails

    NSArray *array = [dataStr componentsSeparatedByString: @","];
    //splits the string into an array by identifying data separators.

    NSLog(@"array: %@", array);
    //prints the array to screen

}

Any further help would be greatly appreciated. Thanks for the answers so far.

+5
source share
3 answers
NSString* fileContents = [NSString stringWithContentsOfURL:filename ...];
NSArray* rows = [fileContents componentsSeparatedByString:@"\n"];
for (...
    NSString* row = [rows objectAtIndex:n];
    NSArray* columns = [row componentsSeparatedByString:@","];
...

, , stringTrimmingCharactersInSet .

+6

:

( ), , . , , @synthesize . , @synthesize @property, . :

@interface MyObject : NSObject {
    NSString *myString;
}
@property (assign) NSString *myString;
@end

@implementation MyObject
@synthesize myString;
  // funky code here
@end

, ( ). . : Objective-C 2.0


, , @interface . @interface, , , .

, :

dataStr = [NSString stringWithContentsOfFile:@"Towers.csv" encoding:NSUTF8StringEncoding error:nil];    
array = [dataStr componentsSeparatedByString: @","];
+1

, - " CSV ", " ". CHCSVParser. . NSArray / . , , .

0

All Articles