Skip to content

Instantly share code, notes, and snippets.

@KentarouKanno
Last active February 16, 2016 14:02
Show Gist options
  • Save KentarouKanno/473103a8e0f4b4f6a332 to your computer and use it in GitHub Desktop.
Save KentarouKanno/473103a8e0f4b4f6a332 to your computer and use it in GitHub Desktop.
変数 定数

Variable, Constant

★ 変数(Variable)

var value = 10

value = 5
//=> 5

★ 定数(Constant)

let value = 10

value = 5
// エラー

★ 複数の初期化

// 複数の変数宣言
var height, width: Int

// 複数の型の初期化
var intValue = 100, doubleValue = 1.23, strValue = "abc"

★ 変数に値を代入

var intValue = 10

var stringValue: String = "abc"

var intArray = Array<Int>([1, 2, 3])

★ 変数に構造体を代入

struct StructSample { }

var structValue = StructSample()

★ 変数に列挙体を代入

enum Blood {
    case A
    case B
    case O
    case AB
}

var enumValue = Blood.A

★ 変数にクラスを代入

class ObjClass {}

var obj = ObjClass()

★ 変数に型を代入

var intType = 10.dynamicType
//=> Int.Type

★ 変数に関数を代入

func method() {
    print("Call Method!")
}

var function = method

// メソッドの実行
function()
//=> Call Method!

// ----------------

class Sample {
    
    // 無名関数を直ぐに実行
    static var name: String = {
        return "Taro"
    }()
}

var name = Sample.name
//=> "Taro"

★ getter, setter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment