如何读取CSV文件中的单独字段并与用户input进行比较?

我正在build立一个应用程序,在这个应用程序中,可以上传包含名字,姓氏和电子邮件的csv文件在3个单独的列中。 用户界面将有3个文本字段和一个button。 当用户input名字或姓氏或电子邮件,然后单击searchbutton时,必须search整个文档,并显示一条提示,说明在该文件中find该logging。 这是我正在使用的function,但它只读取第一行和第一列。 请帮忙

- (void) SearchStudent { 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:@"\n"]; NSArray *paColumnsOfRow; NSString *pstrFirstColumn; for(NSString * pstrRow in paRowsOfCSVFile) { paColumnsOfRow= [pstrRow componentsSeparatedByString:@","]; pstrFirstColumn= [paColumnsOfRow objectAtIndex:0]; if([pstrFirstColumn localizedCaseInsensitiveCompare:GWIDText.text] == NSOrderedSame) { UIAlertView *alertingFileName = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Found" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; [alertingFileName show]; break; } else { UIAlertView *alertingFileName1 = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Not Found" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; [alertingFileName1 show]; break; } } } 

看起来像你的else语句正在摆脱for循环,如果名字不匹配。 如果您find了匹配项,您可能需要删除它,也许还需要添加一个variables。 只有在符合名称的情况下才能设置。 在for循环之后,检查variables以查看是否匹配。 如果没有,则显示您的UIAlertView为“未find”。

几个提示:

  1. 您只在检查一列。

     if([pstrFirstColumn localizedCaseInsensitiveCompare:GWIDText.text] == NSOrderedSame) 

对另外两列做同样的事情。 那么你将解决你的第一个问题,只检查一列。

  1. 看起来行结束符是不正确的。 根据创buildcsv文件的平台,它可能有不同的行结束符。 我会build议采取一个文本编辑器,如记事本+ +例如和查看隐藏的代码找出你的行终止符。 然后使用终结符来拆分行。 确保你获得了多于一行(只输出行variables)。