Question

I know this has been asked before, but there is no answer that I have found useful.

First off here is my code

// load the .csv file with all information about the track

            NSError *error;
            NSString *filepath = [[NSBundle mainBundle] pathForResource:@"file" ofType:@"csv" inDirectory:nil];

            NSString *datastring1 = [NSString stringWithContentsOfFile:filepath encoding:NSUTF8StringEncoding error:&error];
            NSArray *datarow = [datastring1 componentsSeparatedByString:@"\r"];

            //fill arrays with the values from .csv file
            NSArray *data_seg = [datarow objectAtIndex:0]; //segment number
            NSArray *data_slength = [datarow objectAtIndex:1]; //strait length
            NSArray *data_slope = [datarow objectAtIndex:2]; //slope
            NSArray *data_cradius = [datarow objectAtIndex:3]; //circle radius
            NSArray *data_cangle = [datarow objectAtIndex:4]; //circle angle

            NSLog(@"%i", [data_seg count]);

Okay, so there is the code, and I read that is has something to do with autorelease, but I was not able to add a retain like NSArray *data_seg = [[datarow objectAtIndex:0] retain]

When I run the code, I get [__NSCFString count]: unrecognized selector sent to instance 0x9d1ad50

Any help is appreciated, I'm not good at programming, and I am very new.

Was it helpful?

Solution

componentsSeparatedByString method returns an NSArray of NSString. Every item that you extract from datarow array is an NSString and an NSString doesn't respond to 'count'. Your code starting at //fill arrays is incorrect. Every objectAtIndex call will return an NSString*.

This is another way of saying that the datatype for data_seg is NSString* (not NSArray*).

OTHER TIPS

With the corrected code snippet, the problem is because data_seg is a string, and -count is not a method of NSString. It seems you think data_seg is an NSArray.

Look at the documentation for -[NSString componentsSeparatedByString:] and see what it returns -- strings! So you get back an array of strings. So what you want is:

NSString *data_seg = [datarow objectAtIndex:0]; //segment number
NSLog(@"my segment number is: %@", data_seg);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top