LoginSignup
0
1

More than 3 years have passed since last update.

[Swift]定数と変数

Posted at

[Swift]定数と変数

定数と変数は値の一時的な保存に使います。
処理の結果を保存することによって、後続の処理で再利用できます。
変数は値の再代入が可能ですが、定数は一度代入したら値を変更することはできません。

宣言方法

変数はvarで定数はletを使います。

var 変数名:型名
let 変数名:型名

変数名aとbに3を代入します。
Int型で宣言します。

var a:Int=3
let b:Int=3

値の代入方法

次に、aとbに1を代入します。

a=1//1
b=1//コンパイルエラー

結果を見たら一目瞭然ですが、aに1を代入することは可能ですが,bに1を代入することはできません。

0
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
0
1