Skip to content

Instantly share code, notes, and snippets.

@maeharin
Last active August 29, 2015 14:17
Show Gist options
  • Save maeharin/68a972dde9557a7144f0 to your computer and use it in GitHub Desktop.
Save maeharin/68a972dde9557a7144f0 to your computer and use it in GitHub Desktop.
import UIKit
let code: String = "0001"
let num = code.toInt()! + 100
println(num)
// ---
let user_datas = [
["name": "hoge", "age": 20],
["name": "hoge", "age": 30],
["name": "hoge", "age": 40],
]
for user_data in user_datas {
println(user_data["age"]! as Int + 5)
}
// ----
class Area {
let id : Int
var name : String
var title : String?
var stages : [Stage]
init(id : Int, name : String) {
self.id = id
self.name = name
self.stages = []
}
func imagePath() -> String {
return "areas/\(id).png"
}
}
class Stage {
let id : Int
var name : String
var areaId : Int
init(id: Int, name: String, areaId: Int) {
self.id = id
self.name = name
self.areaId = areaId
}
func imagePath() -> String {
return "stage/\(id).png"
}
}
class User {
let id : Int
var name : String
init (id: Int, name: String) {
self.id = id
self.name = name
}
}
let areas = [
Area(id: 1, name: "japan"),
Area(id: 2, name: "USA"),
]
let stages = [
Stage(id: 1, name: "kiyomizudera", areaId: 1),
Stage(id: 2, name: "mt fuji", areaId: 1),
Stage(id: 3, name: "new york", areaId: 2),
Stage(id: 4, name: "missisipi", areaId: 2),
]
let users = [
User(id: 1, name: "hoge"),
User(id: 2, name: "fuge"),
]
for area in areas {
println(area.id)
println(area.title)
println(area.imagePath())
}
for stage in stages {
println(stage.name)
}
for user in users {
println(user.name)
}
// 関数を変数に代入
let foo = {(a: Int, b: Int) -> Int in
return a + b
}
foo(1,2)
// 即時関数の結果を変数に代入
let str = {() -> String in
return "hoge"
}()
let sum = {(a: Int, b: Int) -> Int in
return a + b
}(1,2)
// 即時関数
// jsと同じように()で{}を囲う必要はある
({
println("aaa")
})()
({() -> Void in
println("aaa")
})()
({(a: Int, b: Int) -> Void in
println(a + b)
})(2,2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment