5
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

標準入出力の操作方法(Obj-C)

Last updated at Posted at 2018-07-01

はじめに

「HackerRank」というプログラミングの問題を解くサイトがあります。
https://www.hackerrank.com

こちらのサイトでは「標準入力を取得する→問題を解く→解答結果を標準出力する」という流れで問題を解きます。

Objective-Cで標準入出力を操作する方法に手間取ったので、備忘録として残しておきます。

環境

  • OS:macOS High Sierra 10.13.1
  • Xcode:9.2

プロジェクトの作成

標準入出力を扱うには、コマンドラインツールのプロジェクトを作成します。

Xcodeを起動し、「Create a new Xcode project」をクリックします。
スクリーンショット_2018-07-04_0_05_59.jpg

[macOS]タブにある「Command Line Tool」を選択して[Next]ボタンをクリックします。
スクリーンショット_2018-07-04_0_06_26.jpg

あとは通常のプロジェクトと同様にして作成します。

標準入出力の実装(Obj-C)

コマンドラインツールのプロジェクトはほぼ空のメインファイルのみ存在します。

main.m(before)
#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        NSLog(@"Hello, World!");
    }
    return 0;
}

@autoreleasepool 内を以下のように書き換えます。

main.m(after)
#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // 標準入力を取得する
        NSData *stdinData = [[NSFileHandle fileHandleWithStandardInput] availableData];
        NSString *stdinString = [[NSString alloc] initWithData:stdinData encoding:NSUTF8StringEncoding];
        
        // 標準入力を半角スペース区切りで配列に格納する
        NSArray *stdinArray = [stdinString componentsSeparatedByString:@" "];

        // TODO: 問題を解く
        // ここでは標準入力をそのまま解答とする
        NSArray *result = [stdinArray copy];

        // 解答を半角スペース区切りで文字列とする
        NSString *stdoutString = [result componentsJoinedByString:@" "];
        
        // 解答を標準出力する
        NSFileHandle *fileHandle = [NSFileHandle fileHandleWithStandardOutput];
        [fileHandle writeData:[stdoutString dataUsingEncoding:NSUTF8StringEncoding]];
        [fileHandle writeData:[@"\n" dataUsingEncoding:NSUTF8StringEncoding]];
        [fileHandle closeFile];
    }
    
    return 0;
}

標準入出力のデバッグ

通常のプロジェクトと同様、 ⌘R でデバッグします。

標準入力を受け付けるまで待機するので、画面右下のコンソールに値を入力して Enter を押下します。
スクリーンショット_2018-07-04_0_28_45.jpg

今回は標準入力をそのまま標準出力しているため、標準入力と同じ値が太字で出力されます。
スクリーンショット_2018-07-04_0_30_55.jpg

おわりに

今回作成したプロジェクトをGitHubで公開しました。
https://github.com/uhooi/StdinoutTemplates

実際の出力には NSLog を使うことがほとんどだと思いますし、標準入力は使わないので、このようなプログラミングサイトにしか使わないかもしれません。

参考リンク

5
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?