我正在構建一個應用程序,使用它可以上傳包含名字,姓氏和電子郵件3個獨立列的csv文件。用戶界面將有3個文本字段和一個按鈕。當用戶輸入名字或姓氏或電子郵件,然後單擊搜索按鈕時,必須搜索整個文檔並顯示一條提示,說明在該文件中找到該記錄。這是我正在使用的功能,但它只讀取第一行和第一列。請幫助如何讀取csv文件中的單獨字段並與用戶輸入進行比較?
- (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;
}
}
}
噢亞...我其實沒有別的塊本身試過...但它仍然只是檢查第一行第一列 – dcprog
是什麼\ [paRowsOfCSVFile count \]等於?如果它只有1,並且您的文件有多行,那麼您是否需要在「\ r」而不是「\ n」之間分割? – stevekohls
我使用了\ r ...仍然不工作 – dcprog