ITの隊長のブログ

ITの隊長のブログです。Rubyを使って仕事しています。最近も色々やっているお(^ω^ = ^ω^)

Swiftを勉強してみた。その1

スポンサードリンク

やりたいことがある。それにはアプリを開発しなきゃいけないので、Objective-CはだるいからせっかくなのでSwiftを勉強してみた。

もちろんここで

http://dotinstall.com/lessons/basic_swift

swiftの勉強メモ

コメント 変数宣言

// comment
// 基本的なコメント

/*
multi-line comments

複数行もJavaとかと同じ
*/

// Hello World
// No use single quote シングルクォートは使えない
print("hello world")

// variable
var msg: String // name : Type
msg = "hello world"

print(msg) // ちなみにdotinstallでは"println"であったが、使えなかった。

// one line
var msg2: String = "hello world"

// abridgement
// 一行での変数宣言で、型は省略しても良い
var msg3 = "hello world"

// variable: var
// constant: let
// 多分だけど基本letを使え!ってなると思う

let s = "hoge"

print(s)

// error
// let の場合、同じ名前は使えない
// s = "bar"

型の種類

// base data type

/**
 * String
 * Int
 * Float/Double
 * Bool
 * nil
 */

四則演算

// + - * / %
let x: Float = 8.0 % 2.5

// ++ --
var y = 0
++y // 1
y = 1
--y // 0

// String +
// "hello world"
let ss = "hello " + "world"

比較演算

// && || ! 
true && false // false
true || false // true
!true // false

キャスト

// conversion
let a = "hkt"
let b = 48
// let c = a + b error: No increment int, string type
let c = a + String(b) // "hkt48"

tupple

// # tupple
let error = (404, "not found")
error.0 // 404
error.1 // "not found"

let error1 = (code:404, msg:"not found")
error1.code // 404
error1.msg  // "not found"


let error2 = (404, "not found")
let (code, message) = error2
code // 404
message  // "not found"

// not get message value
let (code2, _) = error2
code2 // 404

配列

// # array

var colors: [String] = ["blue", "pink"]
colors[0] // Subscript : blue
colors[1] // pink
colors[1] = "red"
colors // blue, red

colors.append("green")
colors // blue, red, green
colors.insert("gray", atIndex: 1)
colors // blue, gray, red, green

let secondColor = colors.removeAtIndex(1)
secondColor // gray
colors // blue, red, green

colors.removeLast()
colors // blue, red


// empty array
var emptyArray = [String]()

連想配列(辞書)

// # dictionary
// key: value

var users: [String: Int] = [
    "name": 500,
    "name2": 800
]

users["name"] // 500

users.count // 2

users.isEmpty // false

users["name3"] = 900 // add key:value
users // names:500, names2:800, names3:900

users.removeValueForKey("name3") // key "name3" remove
users  // names:500, names2:800

users.updateValue(900, forKey: "name2")

let keys = Array(users.keys) // name, name2
let values = Array(users.values) // 500, 900

// empty dictionary
var emptyDictionary = [String: Int]()

条件分岐

// # if
let score = 82
var result = ""

if score > 80 {
    result = "Great"
} else if score > 60 {
    result = "Good"
} else {
    result = "so so ..."
}

// ternary operation

result = score > 80 ? "Great" : "so so ..." // Great

// switch
let num = 0
switch num {
case 0:
    print("zero")
    // fallthrough; moving next case context
case 1, 2, 3:
    print("s")
case 4...6: // 4, 5, 6
    print("m")
case 7..<9: // 7, 8
    print("h")
case let n where n > 10:
    print("where")
default:
    print("n.a.")
    
    // break ; not working
}

ループ

// while
var n = 0
while n < 10 {
    print(n)
    n++
}

repeat { // do not working, change "repert"
    print(n)
    n++
} while n < 10

とりあえず、#10までおわった