1

ユーザーがiTunesを介してアプリのドキュメントフォルダーにcsvファイルをアップロードする小さなアプリを開発しています。次のコードを使用していますが、最初の行の最初の列のみをチェックします。2行目の列をチェックしていません。私の csv ファイルには、それぞれ 2 行と 4 列が含まれています。最初の列は数値です。ユーザーはこの番号をテキスト ボックスに入力し、ボタンをクリックして、番号が csv ファイルにあるかどうかを確認します。次の関数は私が使用しているものです

-(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];
        }
    }

}
4

2 に答える 2

0

ループ内にアラートビューを表示しようとしています。したがって、行の数と同じ数のアラートビューを表示しようとすることになります。それはあなたが望むものではありそうにありません。要件が不明確です。すべての行のすべての列をチェックしますか、すべての行の最初の列だけをチェックしますか、それとも単に最初の一致を見つけて停止しますか?

ループの直前にBOOL変数を作成します。のようなものBOOL found = NO;。次に、ループで、一致するものが見つかったら変数をYESに設定します。found次に、ループの後で、変数に基づいて適切なアラートを表示します。正確な解決策は、ここでのあなたの本当の意図に本当に依存します。

于 2012-10-22T02:07:05.100 に答える
0

componentsSeparatedByString: の代わりに componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet] を使用してみて、それが役立つかどうかを確認してください。

于 2012-10-22T04:15:17.333 に答える