17
15

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で文字列内に変数を埋め込む

Posted at

C#、Javaのstring.format的なことをSwiftでしたい場合は『\ ()』 で変数をかこうだけ。

ただ代入する変数がOptionalの場合はどの言語でも同様に注意が必要ですね。
テストを怠って②、③のような結果を人に見られたら赤面モノです。(体験談)

// ①
var c1 : Int = 3
println("この投稿は\(c1)回目の投稿です。")  //この投稿は3回目の投稿です。

// ②
var c2 : Int? = 3
println("この投稿は\(c2)回目の投稿です。")  //この投稿はOptional(3)回目の投稿です。

// ③
var c3 : Int? = nil
println("この投稿は\(c3)回目の投稿です。")  //この投稿はnil回目の投稿です。
17
15
2

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
17
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?