swift函数形参默认值 ,for in 中的_,inout 关键字小结,func,闭包

func join(string s1: String, toString s2: String,withJoiner joiner: String = " ") -> String
{
  return s1 + joiner + s2
}
 

join(string: "hello", toString: "world", withJoiner: "-")

 // returns "hello-world" 

 

 join(string: "hello", toString: "world")

// returns "hello world"

---------------------------------------------------------------------------------------------------------

如果不需要知道范围内每一项的值,你可以使用下划线(_)替代变量名来忽略对值的访问 

 for _ in something

{

  //something

}

---------------------------------------------------------------------------------------------------------

In-out 形参不同于从函数返回一个值。对函数来说,In-out 形参是一个影响函数主体范围外的可选方式。

--------------------------------------------------------------------------------------------------------- 

func addTwoInts(a: Int, b: Int) -> Int
{
 return a + b

mathFunction: (Int, Int) -> Int = addTwoInts 

----------------------------------------------------------------------------------

 全局函数是一个有名字但不会捕获任何值的闭包

 嵌套函数是一个有名字并可以捕获其封闭函数域内值的闭包

 闭包表达式是一个利用轻量级语法所写的可以捕获其上下文中变量或常量值的没有名字的闭包

Swift 闭包:

利用上下文推断参数和返回值类型

单表达式(single-expression)闭包可以省略 return 关键字 * 参数名称简写

Trailing 闭包语法 

posted on 2015-04-21 18:04  DeanLee  阅读(443)  评论(0)    收藏  举报