swift tour

it2026-02-02  3

// Playground - noun: a place where people can play import UIKit var str = "Hello, playground" println(str + " " + str) let a = 1 var varDouble: Double = 60 varDouble = 70 var varFloat: Float = 62 let width = "the window width is" var b: Int32 = 2 let 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 = 0 for score in allScores { if score < 20 { teamScore += 3 } else { teamScore += 6 } } teamScore var optioncalString: String? = "Hello" optioncalString = nil if let name = optioncalString { optioncalString = "hello \(varFloat)" } else { optioncalString = "hello \(allScores[1])" } // switch support any kind of data let 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 type var interesingNum = [ "Prime": [2,3,5,7,11,13], "Fibonacci": [1,1,2,3,5,8], "Square": [1, 4, 9, 16, 25] ] var largest = 0 for (k, nums) in interesingNum { for num in nums { if num > largest { largest = num } } } largest var n = 2 while n < 100 { n *= 2 } n var m = 1 do { m *= 2 } while m < 100 m var firstForLoop = 1 for i in 1..<4 { firstForLoop += 1 } firstForLoop var secondForLoop = 1 for i in 1...4 { secondForLoop += 1 } secondForLoop func 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.sum statistics.0 func 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 type func 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 }) ttnums let mappedNums = numbers.map({number in number * 3}) let sortedNums = sorted(numbers) {$0 < $1} sortedNums class 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?.numberOfSides shape?.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.queen let rawace = ace.rawValue let 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.simpleDescription func repeat<ItemType>(item: ItemType, times: Int) -> [ItemType] { var result = [ItemType]() for i in 0..<times { result.append(item) } return result } repeat("knock", 3) enum OpticalValue<T> { case None case Some(T) } var possibleInterger:OpticalValue<Int> = .None possibleInterger = .Some(100) func anyCommonElements<T, U, where T: SequenceType, U: SequenceType, T.Generator.Element: Equatable, T.Generator.Element == U.Generator.Element> (lhs: T, rhs: U) -> Bool { for litem in lhs { for ritem in rhs { if litem == ritem { return true } } } } anyCommonElements([1, 3, 5], [3])

转载于:https://www.cnblogs.com/octave/p/4422782.html

最新回复(0)