6

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

I'm trying to plot coordinates on a map.

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

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

the second, NSArray *region (containing tower title and region) with the same count index as the first array.

Essentially, I believe I need to;

1) read the file to a string.....

2) divide the string into a temporary array separating at every /n/r......

3) loop through the temp array and create a tower and region object each time before appending this information to the two main storage arrays.

Is this the right process and if so is there anyone out there who can post some sample code as I'm really struggling to get this right.

Thanks to all in advance.

Chris.

I have edited this to show an example of my code. I am having the problem that I'm receiving warnings saying

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

I think I understand what these mean but I don't know how to get around it. 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 additional help would be much appreciated. Thanks for the responses so far.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
user1341967
  • 61
  • 1
  • 1
  • 3
  • first you have to do some thing by your side and if u stuck some where between those tries post code and then we can help you out so good luck with your tries – The iOSDev Apr 18 '12 at 16:57
  • Can you give a couple example lines of the CSV file and explain where the info is? – NSGod Apr 18 '12 at 17:03

3 Answers3

8
NSString* fileContents = [NSString stringWithContentsOfURL:filename ...];
NSArray* rows = [fileContents componentsSeparatedByString:@"\n"];
for (...
    NSString* row = [rows objectAtIndex:n];
    NSArray* columns = [row componentsSeparatedByString:@","];
...

You'll probably want to throw in a few "stringTrimmingCharactersInSet" calls to trim whitespace.

Hot Licks
  • 47,103
  • 17
  • 93
  • 151
1

Concerning your warnings:

Your code would produce an error (not a warning), since you need to declare your properties in the interface file before you synthesize them in the implementation. You probably remember that @synthesize generates accessor methods for your properties. Also, before using the @synthesize directive, you need to use the @property directive, also in the interface. Here's an example:

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

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

Note that the property declaration is followed by a type (assign in this case, which is the default). There's an excellent explanation about this in Stephen G. Kochans's book: Programming in Objective-C 2.0


But assuming for argument's sake, that you omitted the correct @interface file here. If you first declare a property in the @interface, and then declare another property in your method, using the same variable name, the method variable will take precedence over the instance variable.

In your code, it would suffice to omit the declaring of the variable name, like so:

dataStr = [NSString stringWithContentsOfFile:@"Towers.csv" encoding:NSUTF8StringEncoding error:nil];    
array = [dataStr componentsSeparatedByString: @","];
Fnord23
  • 325
  • 2
  • 18
0

I'm assuming that the core of your question is "how to parse a CSV file", not "what to do with the data once it's parsed". If that's the case, then check out the CHCSVParser library. I have used it in projects before and find it to be very reliable. It can parse any arbitrary string or filepath into an NSArray of rows/columns for you. After that, whatever you do with the data is up to you.

Matt J
  • 961
  • 6
  • 4