// Playground - noun: a place where people can playimport UIKitvar str = "Hello, playground"println(str + " " + str)let a = 1var varDouble: Double = 60varDouble = 70var varFloat: Float = 62let width = "the window width is"var b: Int32 = 2let printstr = width + String(b)println(printstr)println("just output : \(varDouble)")var shopinglist = ["apple", "orange", "tomato"]shopinglist[1]var occupation = [ "lili":"beijing", "tom":"shanghai"]occupation["lili"]let emptyArray = [String]()let emptyDict = [String: Float]()shopinglist = []occupation = [:]let allScores = [16, 19, 21, 35, 66]var teamScore = 0for score in allScores { if score < 20 { teamScore += 3 } else { teamScore += 6 }}teamScorevar optioncalString: String? = "Hello"optioncalString = nilif let name = optioncalString { optioncalString = "hello \(varFloat)"} else { optioncalString = "hello \(allScores[1])"}// switch support any kind of datalet vegetable = "red pepper"switch vegetable { case "celery": let vegetableComment = "买二斤红辣椒" case "cucumber", "watercress": let vegetableComment = "买黄瓜" // case x let x.hasSuffeix("pepper"): // let a = 1 default: let b = 2}// same typevar interesingNum = [ "Prime": [2,3,5,7,11,13], "Fibonacci": [1,1,2,3,5,8], "Square": [1, 4, 9, 16, 25]]var largest = 0for (k, nums) in interesingNum { for num in nums { if num > largest { largest = num } }}largestvar n = 2while n < 100 { n *= 2}nvar m = 1do { m *= 2} while m < 100mvar firstForLoop = 1for i in 1..<4 { firstForLoop += 1}firstForLoopvar secondForLoop = 1for i in 1...4 { secondForLoop += 1}secondForLoopfunc greet(name: String, day: String) -> String { return "Hello \(name), today is \(day)"}greet("Bob", "Monday")func calcuateStatistics(score: [Int32]) -> (min:Int32, max:Int32, sum:Int32) { var min = score[0] var max = score[0] var sum: Int32 = 0 return (min, max, sum)}func greet(name: String, day: String) -> String { return "Hello \(name), today is \(day)"}greet("Bob", "Monday")func calcuateStatistics(score: [Int]) -> (min:Int, max:Int, sum:Int) { var min = score[0] var max = score[0] var sum: Int = 0 for num in score { if num < min { min = num } else if num > max { max = num } sum += num } return (min, max, sum)}let statistics = calcuateStatistics([5, 3, 100, 3, 9])statistics.sumstatistics.0func sumOf(nums: Int...) -> Int { var sum = 0 for num in nums { sum += num } return sum}sumOf(1, 2, 3, 4, 5, 6)// functions are first-class typefunc makeIncrementer() -> (Int -> Int) { let one = 1 func addOne(num: Int) -> Int { return num + one } return addOne}var increase = makeIncrementer()increase(6)func hasAnyMatch(list:[Int], condition: Int->Bool) -> Bool { for value in list { if condition(value) { return true } } return false}func lessThanTen(num: Int)->Bool { return num < 10}hasAnyMatch([2, 5, 6, 12, 16, 19, 27], lessThanTen)var numbers = [1, 1, 2, 3, 5, 8, 13, 21]let ttnums = numbers.map({ (num: Int) -> Int in let result = num * 3 return result})ttnumslet mappedNums = numbers.map({number in number * 3})let sortedNums = sorted(numbers) {$0 < $1}sortedNumsclass NamedShape { var numberOfSides = 0 var name: String init(name: String) { self.name = name } deinit { } func simpleDescription() -> String { return "A shape with \(numberOfSides) sides" }}class Square: NamedShape { var sideLength = 0.0 var perimeter: Double { get { return self.sideLength * 3 } set { sideLength = newValue / 3 } } init(sideLength: Double, name: String) { super.init(name: name) self.sideLength = sideLength numberOfSides = 4 } func aera() -> Double { return sideLength * sideLength } override func simpleDescription() -> String { return "A shape with side of length \(sideLength)" }}let shape: Square? = Square(sideLength: 6.1, name: "A-square")shape?.aera()shape?.numberOfSidesshape?.simpleDescription()/enum Rank: Int { case Ace = 1 case Two, third, four, five, six, seven, eight, nine, ten case jack, queen, king func simpleDiscription() -> String { switch self { case .Ace: return "ace" case .queen: return "queen" default: return String(self.rawValue) } }}let ace = Rank.queenlet rawace = ace.rawValuelet discription = ace.simpleDiscription()protocol ExampleProtocol { var simpleDescription: String { get } mutating func adjust() // mutating: function modifies the struct}class simpleProtocol: ExampleProtocol { var simpleDescription: String = "A simple protocol" var anotherProperty: Int = 69105 func adjust() { simpleDescription += "now 100% adjust" }}extension Int : ExampleProtocol { var simpleDescription: String { return "the number \(self)" } mutating func adjust() { self += 42 }}7.simpleDescriptionfunc repeat (item: ItemType, times: Int) -> [ItemType] { var result = [ItemType]() for i in 0.. { case None case Some(T)}var possibleInterger:OpticalValue = .NonepossibleInterger = .Some(100)func anyCommonElements (lhs: T, rhs: U) -> Bool { for litem in lhs { for ritem in rhs { if litem == ritem { return true } } }}anyCommonElements([1, 3, 5], [3])