4
2

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.

【Swift2.0】String型をInt型に変換する

Last updated at Posted at 2015-11-19

Swift2.0でString型をInt型に変換する

※勉強用メモ

背景
toInt関数がSwift2.0で使えなくなった。
そのため代替え方法を調べた。

以下の記述にすることでString型からInt型への型変換が可能に。

注意点
数値に変換できない文字列を変換しようとするとnilが返ってくるので注意。

コード


   let strVal: String! = "123"
if let intVal: Int? = Int(strVal) {
   print(intVal)
}

結果


intVal : 123

Swift2.0の情報が少なすぎる。。。

補足

サンプルコードにおいてnilの場合の考慮と定数宣言にする箇所ができていなかった為、修正しました。

4
2
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
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?