27
24

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.

[改編]Swiftを使ってPaizaの問題を解く

Last updated at Posted at 2016-02-13

Paizaの問題をSwiftで解こうと思った際、
分からなかったポイントがあったので、共有します。

[2016.5.17追記]
単体でswiftのファイルを作成してもXcode上の補完機能が、全く効かないため
Command Line Toolsを使う方法を追記し、修正させていただきました。

1. Command Line Toolsを使ったプロジェクトを作成する

xcodeを起動し、cmd + shift + N かランチャーのメニューから
新しいprojectを作成し、
OS X > Command Line Toolを選択します。

commadLineTools.png

これでちゃんとコードの補完が効きます。
hokan.png

2. 標準入力を受け取る

let input_line = readLine()!

で、OK。(swift2.0)
標準出力は普通にprint

3. デバッグする

ターミナルを開いて、コマンドを叩くか

swift test.swift

cmd + Rでbuildしたあとに、デバッグコンソールエリアにテキストを入力しても
標準入力を取得しつつ、デバッグができます。

4. 文字列をsplitする

Paizaの問題にはめちゃくちゃ空白区切りの文字列をsplitする問題が多いです。
Swift(2.0)にはcomponentsSeparatedByStringという関数があるので、それを使います。

let input_line = readLine()!
let arr = input_line.componentsSeparatedByString(" ")

参考URL

readLine() in playgrounds
https://forums.developer.apple.com/thread/37057

以上です。
これで、Paiza解答用の基本的な環境は整ったかと思います。

27
24
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
27
24

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?