現在、次のコードを使用して、標準入力から最初の文字を読み取ります(プロンプトの後)。
NSFileHandle *stdIn = [NSFileHandle fileHandleWithStandardInput];
NSString* input = [[NSString alloc]
initWithData:[stdIn readDataOfLength:1]
encoding:NSUTF8StringEncoding];
ただし、プログラムの完了後、余分な文字はすべてコマンドライン(この場合はbash)によって解釈されます。たとえば、プロンプトは
File already exists, do you want to overwrite it? [y/N]
入力するnoooooo
と、bashは-bash: oooooo: command not found
プログラムの終了後に言います。これを回避するにはどうすればよいですか?予想通り、使用[stdIn readDataToEndOfFile]
は機能しません。
を使用することはできます[stdIn readToEndOfFileInBackgroundAndNotify]
が、そのようなプロンプトからユーザー入力を再度取得する必要がある場合、それは不可能です。