博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
swift tour
阅读量:5040 次
发布时间:2019-06-12

本文共 5797 字,大约阅读时间需要 19 分钟。

// 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])

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

你可能感兴趣的文章
PHP学习 Object Oriented 面向对象 OO
查看>>
转载 .net中的dll.refresh文件和pdb文件
查看>>
python 缩进问题
查看>>
黑马程序员 一个准程序的内心告白,原来上帝是那么的遥远
查看>>
铺地毯
查看>>
Vue.js---组件
查看>>
C++ 中超类化和子类化
查看>>
CEF3开发者系列之进程间消息传递
查看>>
Android 网络加载通用Loading
查看>>
hdu 1756:Cupid's Arrow(计算几何,判断点在多边形内)
查看>>
Eclipse+php插件+Xdebug搭建PHP完美开发/调试环境指南
查看>>
时序分析 基本术语 摘记 (ALTERA 官方教程)
查看>>
分治算法经典案例 - 棋盘问题
查看>>
A Mysterious 'RuntimeLibrary' Link Error
查看>>
复合数据类型
查看>>
[Lintcode]163. Unique Binary Search Trees
查看>>
mysql主从
查看>>
[转]C# 常用字符串加密解密方法
查看>>
SQL常用语句
查看>>
Project Euler Problem 5-Smallest multiple
查看>>