Created
May 29, 2015 00:02
-
-
Save mattn/94c90ed66771dd55dff2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* コメントはCスタイルの複数行コメントと… | |
*/ | |
// C++スタイルの一行コメントの双方をサポートしている | |
// var name:Type = value でType型の変数nameを宣言し、valueで初期化する | |
var explicitDouble:Double = 70 // 70.0 | |
/// 型が省略された場合は、型推論により初期値の型が適用される | |
var implicitInteger = 70 // Int | |
var implicitDouble = 70.0 // Double | |
// let name:Type = value でType型の定数nameにvalueを設定する。 | |
// 型推論可能な場合、型の表記は省略できる。 | |
let theAnswer = 42 | |
// 識別子にはたいていのUnicode文字を用いることができる。 | |
let リンゴの数 = 3 | |
let みかんの数 = 5 | |
// 文字列リテラル"..."の中にある\(expr)には、式exprの内容が展開される | |
let リンゴ説明 = "私は\(リンゴの数)個のリンゴを持っている。" // ”私は3個のリンゴを持っている。" | |
let 果物説明 = "私は\(リンゴの数 + みかんの数)個の果物を持っている。" //"私は8個の果物を持っている。" | |
// Swiftでは辞書も組み込みでサポートされている。 | |
// 以下は Dictionary<String, Int> 型の定数辞書の定義の一例である。 | |
let people = ["Anna": 67, "Bety": 8, "Jack": 33, "Sam": 25] | |
// 辞書の内容の列挙は for (key, value) in dict { ... } | |
for (name, age) in people { | |
println("\(name) is \(age) years old.") | |
} | |
// メソッドや関数は "func"文法を使って宣言する。 | |
// パラメータ名の付け方に注意。-> で戻り値の型を宣言する | |
func sayHello(personName: String) -> String { | |
let greeting = "こんにちは、" + personName + "さん" | |
return greeting | |
} | |
// "こんにちは、花子さん"を出力 | |
println(sayHello("花子")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment