It's How different
File: MyPlayground.swift ------------------------ // Playground - noun: a place where people can play import UIKit var str = "Hello, playground" // コンソール上にHello Worldの表示 println("Hello, world!") // 変数 var myVariable = 42 // 再代入可能 myVariable = 50 // 定数, 再代入不可 let myConstant = 42 let implicitInteger = 70 let implicitDouble = 70.0 // 型指定, 型アノテーションという let explicitDouble: Double = 70 let label = "The width is " let width = 94 // 型変換、明示的に変換する必要がある let widthLabel = label + String(width) let desc = "My Height is" let height = 187 // Unicode文字が使える、絵文字、日本語可能 let 名前 = "マツオカ" // これはNG //let myHeight = desc + height // 文字列内、展開 let apples = 3 let oranges = 5 let appleSummary = "I have \(apples) apples." let fruitSummary = "I have \(apples + oranges) pieces of fruit." let floatVal: Float = 0.2 let myheight = 187 let Summary = "I'm \(myheight + Int(floatVal)) tall." // 配列と辞書 var shoppingList = ["catfish", "water", "tulips", "blue paint"] shoppingList[1] = "bottle of water" var occupations = [ "Malcolm": "Captain", "Kaylee": "Mechanic", ] occupations["Jayne"] = "Public Relations" // 配列、辞書の初期化 let emptyArray = [String]() let emptyDictionary = [String: Float]() shoppingList = [] occupations = [:] // タプル(複数の値を一つの混合値として定義) // 関数から複数の値を一度に返したい時はタプルを使用 let http404Error = (404, "Not Found!") // 数字でのアクセス println("ステータスコードは、\(http404Error.0)") println("ステータスメッセージは、\(http404Error.1)") // 要素に名前でのアクセス let http200Status = (statusCode: 200, description: "OK") println("ステータスコードは、 \(http200Status.statusCode)") println("ステータスメッセージは、 \(http200Status.description)") // 条件分岐 let individualScores = [75, 43, 103, 87, 12] var teamScore = 0 for score in individualScores { if score > 50 { teamScore += 3 } else { teamScore += 1 } } // Optional // nilになりうる値を管理するためのOptional // Optionalとはnilを許容する var optionalString: String? = "Hello" optionalString == nil var optionalName: String? = "John Appleseed" var greeting = "Hello!" optionalName = nil if let name = optionalName { greeting = "Hello, \(name)" } // ?を取るとどうなる? // 連想配列の展開と評価 let interestingNumbers = [ "Prime": [2, 3, 5, 7, 11, 13], "Fibonacci": [1, 1, 2, 3, 5, 8], "Square": [1, 4, 9, 16, 25], ] var largest = 0 for (kind, numbers) in interestingNumbers { for number in numbers { if number > largest { largest = number } } } largest // forループ var firstForLoop = 0 for i in 0..<4 { firstForLoop += i } firstForLoop var secondForLoop = 0 for var i = 0; i < 4; ++i { secondForLoop += i } secondForLoop // 一つ目と2つ目は同じ // 関数とクロージャー // 関数の名前、パラメータ名、型、返り値の型 func greet(name: String, day: String, todaysLunch: String) -> String { return "Hello \(name), today is \(day). today's lunch is \(todaysLunch)." } greet("Bob", "Tuesday", "Steak sandwich") // 複数の返り値を返す関数 func calculateStatistics(scores: [Int]) -> (min: Int, max: Int, sum: Int) { var min = scores[0] var max = scores[0] var sum = 0 for score in scores { if score > max { max = score } else if score < min { min = score } sum += score } // タプルで返す return (min, max, sum) } let statistics = calculateStatistics([5, 3, 100, 3, 9]) // 返り値の名前での参照 statistics.sum // 数字での参照 statistics.2 // 可変引数 func sumAndAverage(numbers: Int...) -> (sum: Int, average: Int) { var sum = 0 var average = 0 for number in numbers { sum += number } if sum > 0 { average = sum / numbers.count } return (sum, average) } println(sumAndAverage(42, 597, 12).sum) println(sumAndAverage(42, 597, 12).average) // ネストした関数 func makeIncrementer() -> (Int -> Int) { func addOne(number: Int) -> Int { return 1 + number } // 返り値を関数にできる return addOne } var increment = makeIncrementer() increment(7) // 引数に関数を使用 func hasAnyMatches(list: [Int], condition: Int -> Bool) -> Bool { for item in list { if condition(item) { return true } } return false } func lessThanTen(number: Int) -> Bool { return number < 10 } var numbers = [20, 19, 7, 12] hasAnyMatches(numbers, lessThanTen) // クロージャー {}で囲む numbers.map({ (number: Int) -> Int in let result = 3 * number return result }) let mappedNumbers = numbers.map({ number in 3 * number }) mappedNumbers let sortedNumbers = sorted(numbers) { $0 > $1 } sortedNumbers