I am developing a small app where the user uploads a csv file to the document folder in the app through iTunes. I am using the following code, but it checks only the first column in first row. Its not checking the second rows column. My csv file contains 2 rows and 4 columns each. First column is a number. The user will type this number in a text box and click a button to check whether the number is in csv file. The following function is the one I am using
-(void)CheckEntry //this function checks the example.csv file
{
NSArray *DocumentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *DocumentDirectory = [DocumentPath objectAtIndex:0];
NSString *FullPath = [DocumentDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"example.csv"]];
NSString * pstrCSVFile= [NSString stringWithContentsOfFile:FullPath encoding:NSASCIIStringEncoding error:NULL];
NSArray * paRowsOfCSVFile= [pstrCSVFile componentsSeparatedByString:@"\r\n"];
NSArray *paColumnsOfRow;
NSString *pstrFirstColumn;
for(NSString * pstrRow in paRowsOfCSVFile)
{
paColumnsOfRow= [pstrRow componentsSeparatedByString:@","];
pstrFirstColumn= [paColumnsOfRow objectAtIndex:0];
if([pstrFirstColumn localizedCaseInsensitiveCompare:GWIDText.text] == NSOrderedSame)
{
NSString *msg = [NSString stringWithFormat:@"The record was found"];
UIAlertView *alertingFileName = [[UIAlertView alloc]initWithTitle:msg message:msg delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alertingFileName show];
}
else
{
NSString *msg = [NSString stringWithFormat:@"Not Found"];
UIAlertView *alertingFileName = [[UIAlertView alloc]initWithTitle:msg message:msg delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alertingFileName show];
}
}
}
Try using componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet] instead of componentsSeparatedByString: and see if that helps.