2021-02-24から1日間の記事一覧

オブジェクトとクラス クラス定義してインスタンス化して利用

/* オブジェクトとクラス クラス定義してインスタンス化して利用 */ class Shape { var numberOfSides = 0 func simpleDescription() -> String { return "A shape with \(numberOfSides) sides." } } var shape = Shape() shape.numberOfSides = 7 print("s…

複数の戻り値の関数

/* 複数の戻り値の関数 */ func count(string: String) -> (vowels: Int, consonants: Int, others: Int) { var vowels = 0, consonants = 0, others = 0 for character in string { // switch String(character).lowercaseString { // error: value of type…

関数が戻り値になる関数

/* 関数が戻り値になる関数 */ // func makeIncrementer() -> (Int -> Int) { // error: single argument function types require parentheses func makeIncrementer() -> ((Int) -> Int) { func addOne(number: Int) -> Int { return 1 + number } return a…

funcで関数の定義と代入

/* funcで関数の定義と代入 */ // 関数を定義して func greet(name: String, day: String) -> String { return "Hello \(name), today is \(day)." } // 代入もできる。 // greet("Bob", "Tuesday") // error: missing argument labels 'name:day:' in call …

NULLも入る変数の宣言

/* NULLも入る変数の宣言 */ var flag: Bool? var num: Int? if (flag == nil) { print("flag == NULL") } // if (flag) { // error: value of optional type 'Bool?' must be unwrapped to a value of type 'Bool' if (flag ?? false) { print("flag == tru…

変数の型宣言と型推論

/* 変数の型宣言と型推論 */ var welcomeMessage: String var pi: Double = 3 var piFl: Float = 3 var piInt: Int = 3 var piAuto = 3 pi = 3.14 piFl = 3.14 // piInt = 3.14 // error: cannot assign value of type 'Double' to type 'Int' // piAuto = 3…

変数の宣言 varは変数、letは定数

/* 変数の宣言 varは変数、letは定数 */ var myVar = 42 myVar = 50 let myConst = 42 // myConst = 50 // error: cannot assign to value: 'myConst' is a 'let' constant print("Hello, world!") print("myVar = \(myVar), myConst = \(myConst)")