Swift ざっくり文法 (1)

  • statement

    • 区切りは、; か改行
    • ; 付けないことが一般的
  • print

print("blue", "green", separator: "+", terminator: ",")
print("yellow", terminator: ",")
print("red")
// blue+green,yellow,red
print(#file) // MyPlayground.playground
print(#line) // 16
print(#column) // 7
print(#function) // __lldb_expr_57
  • constant, variable

    --- ---
    constant let
    variable var
    • {} で区切られている範囲がconstant, variableのスコープ
      • class
      • method
      • if statement
let name: String
name = "タナカ"
let name: String = "タナカ"
let name = "タナカ"
  • 演算子の、前後に空白を入れるか/詰めるかしないとError
price = 2500 * rate // ok
price = 2500*rate // ok
price = 2500* rate // error: consecutive statements on a line must be separated by ';'
  • 型判定
let tax = 0.08
print(type(of:tax)) // Double
  • 型変換
let num: Int = 4 // 4
Double(num) // 4.0
  • 文字列中の変数展開
let hoge = 32
print("hoge: \(hoge)")

Reference from

https://www.amazon.co.jp/dp/4800711843