swift 个人笔记

时间:2022-09-16 07:55:09

swift是现代编程语言的综合体,灵活而强大。

//http://www.cocoachina.com/newbie/basic/2014/0604/8675.html

//语法var 变量,let 变量

var param1 = 5

var param2: Double = 6.0

let param3 = "5 + 6 = \(param1+param2)"

// 字典dict,key是String,value是Float的数组

var dict = Dictionary<String,Array<Float>>()

dict["football_score"] = [5.5,9.0,4]

dict["basketball_score"] = [99,101,87,73]

var largest: Float = 0.0

for (keys,values) in dict{

  for score in values{

    if largest< score{

      largest = score

    }

  }

}

var firstForLoop = 0

// for in 中的 0..3,是 i=0;i<3。0...3,是 i=0;i<=3

for i in 0...3 {

  firstForLoop += i

}

语法

//http://www.cocoachina.com/newbie/basic/2014/0607/8717.html

//func作为函数时,func可以当函数参数、函数返回值,func在某种角度上可以对象去理解

func function1(name: String[],fun: (String,Int)->String) -> (Int,Int)->Bool{

func afn(param: Int,param2: Int)->Bool{

return param>param2

}

//内联函数afn可以简写成下面。返回类型为函数时,该函数会保留上下文作用域内的,变量常量的值;返回的函数相当于一个对象,作用域内的值相当于对象的属性

return {(param: Int,param2: Int)-> Bool in return param>param2}

}