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

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

複数の戻り値の関数

/*
  複数の戻り値の関数
*/
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 'String' has no member 'lowercaseString'
        switch String(character).lowercased() {
        case "a", "e", "i", "o", "u":
//          ++vowels // error: use of unresolved operator '++'; did you mean '+= 1'?
            vowels += 1
        case "b", "c", "d", "f", "g", "h", "j", "k", "l", "m",
        "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z":
            consonants += 1
        default:
            others += 1
        }
    }
    return (vowels, consonants, others)
}
// let total = count("some arbitrary string!") // error: missing argument label 'string:' in call
let total = count(string: "some arbitrary string!")
print("total = \(total)")

関数が戻り値になる関数

/*
  関数が戻り値になる関数
*/
// func makeIncrementer() -> (Int -> Int) { // error: single argument function types require parentheses
func makeIncrementer() -> ((Int) -> Int) {
    func addOne(number: Int) -> Int {
        return 1 + number
    }
    return addOne
}
var increment = makeIncrementer()
// increment(7) // warning: result of call to function returning 'Int' is unused
print("increment(7) = \(increment(7)).")

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
// greet(name: "Bob", day: "Tuesday") // warning: result of call to 'greet(name:day:)' is unused
print(greet(name: "Bob", day: "Tuesday"))

// 外部引数名省略の関数
func greet_omit(_ name: String, _ day: String) -> String {
    return "Hello \(name), today is \(day)."
}
print(greet_omit("Bob", "Tuesday"))

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 == true")
} else {
    print("flag == false")
}
flag = true
if (flag ?? false) {
    print("flag == true")
} else {
    print("flag == false")
}
// print("num = \(num)") // warning: string interpolation produces a debug description for an optional value; did you mean to make this explicit?
print("num = \(num ?? 0)")

変数の型宣言と型推論

/*
  変数の型宣言と型推論
*/
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.14 // error: cannot assign value of type 'Double' to type 'Int'

変数の宣言 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)")