3
3

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.

apple swift 変数(variable)の基礎

Last updated at Posted at 2014-06-22

変数の定義

変数を指定する際、型を指定しなくても使えるのが便利です。

型指定なし
【書式】var 変数名 = 値

var fruits1 = "apple"
var fruits2 = "banana"

型指定あり
【書式】var 変数名:型 = 値

var tmp1:String = "str"
var tmp2:Int = 5
var tmp3:Double = 3.14
var tmp4:Bool = true

swiftの代表的な値型には Int(整数型)Double(浮動小数点数型)String(文字列型)Character(文字型)Bool(真偽型)があります

変数の引用

バックスラッシュ、括弧を使用
macからバックスラッシュを入力する方法: option+¥

var like_fruits = "I like \(fruits1) and \(fruits2)"

// 一つの格好で文字連結は
var like_fruits = "I like \(fruits1 + fruits2)"

// int型同士の計算も可能
var num1 = 1
var num2 = 3
var result = "1 + 3 = \(num1+num2)"

定数の定義/引用

定数も変数同様型を指定しなくても使えます。
ただ、先頭にvarではなくletで書くのが違いますね。

let URI:String = "http://test.jp"
let URI2 = "http://test2.jp"

定数の引用も同じです。

var result2 = "\(URI)/?param=a"

簡単に変数の指定、引用ができるので、便利ですね。
playgroundを使うと変数の中身も自動的に表示されるので効率よく開発ができる気がしました!!

IOS8アプリ開発日誌掲載中!!

3
3
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
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?