読者です 読者をやめる 読者になる 読者になる

今日の学習

はてな教科書よりわかり易い。踏み込んでいないからか。
元来、言語の全仕様を駆使してプログラムするべきじゃないと思うの。
基本のデータの扱い方を学んでどの言語でも使えるようにやるのがベストプラクティスだとぼくは思うの

import UIKit

var str = "Hello, playground"

let color = ["red","blue","yellow"]

print(str)

for (index,value) in color.enumerate() {
    print("\(index),\(value)")
}

let msg:String? = "hoge"
print(msg!);
if let m = msg {
    print(m)
}

func hello(){
    print("helloworld")
}
hello()


func makeGene<T>(items:T...) -> [T] {
    var array = [T]()
    for item in items {
        //array += [item]
        array.append(item)
    }
    return array
}

let collors = makeGene("res","aefe","sde")

let cs = ["RED","BUl"]
let cs2 = cs.map(){str in str.lowercaseString}
let cs3 = cs.map {str in str.lowercaseString}
print(cs2)
print(cs3)


// クロージャ
func battery(capacity:Int) -> (Int) -> Int {
    var stock = capacity

    func use( volume : Int) -> Int {
        if(stock > volume) {
            stock -= volume
        }else {
            stock = 0
        }
        print(stock)
        return stock;
    }

    return use
}

let battery100 = battery(1000)
battery100(100)
battery100(100)

//アクセス権どうなっとるの?
class MyClass {

    let msg = "ハロー"
    private var msg2 = "ハロー"

    // computed プロパティ
    var doubleMsg : String {
        get {
            return msg2 + ":" + msg2;
        }
        set (msg) {
            msg2 = msg
        }
    }


    func hello(){
        print(msg)
    }
    private func hello2(){
        print(msg2)
    }

    // クラスメソット
    class func hello(){
        print("aaaa")
    }

}

let m = MyClass()
m.hello()
m.msg
m.msg2
m.hello2()
MyClass.hello()
print(m.doubleMsg)

// struct with subscript
struct Stock {
    var name : String
    var data : [String:Int] = [:]

    init(name:String){
        self.name = name
    }

    subscript(color:String,size:Double) -> Int {
        get {
            let key = color + size.description
            if let value = data[key] {
                return value
            }else{
                return 0
            }
        }
        set {
            let key = color + size.description
            data[key] = newValue
        }
    }
}
var addixStock = Stock(name:"Shoes")
addixStock["red",26.0] = 30
addixStock["blue",23.5] = 40
addixStock["red",26.0] += 30
print(addixStock["red",26.0])