Chinaunix首页 | 论坛 | 博客
  • 博客访问: 257865
  • 博文数量: 42
  • 博客积分: 2415
  • 博客等级: 大尉
  • 技术积分: 590
  • 用 户 组: 普通用户
  • 注册时间: 2006-01-13 14:14
文章分类

全部博文(42)

文章存档

2018年(1)

2017年(8)

2015年(3)

2012年(4)

2011年(11)

2010年(1)

2009年(5)

2008年(9)

我的朋友

分类: iOS平台

2015-12-08 23:19:55

1.To use a function, you “call” that function with its name and pass it input values (known as arguments) that match the types of the function’s parameters. A function’s arguments must always be provided in the same order as the function’s parameter list.
这里说明了在swift里parameter和argument的语义。按照传统中文说法,argument是实参,parameter是形参,当然重点并不在于中文怎么说,而是在于将来提到parameter和argument的时候能够知道指的是什么。
2.
有返回值的函数声明
  1. func function name(parameters) -> return type {

  2. statements

  3. }
没有定义返回类型的函数声明
  1. func function name(parameters) {

  2. statements

  3. }

严格说起来,没有定义返回类型的函数仍然会返回一个值,这个返回值的类型是Void ,一个空的tuple,可以写成().

3.函数的parameter有两个名字:external parameter name 以及 local parameter name.在调用函数时使用external parameter name,在函数体内使用local parameter name,虽然两者可能会是同样的。

  1. func someFunction(firstParameterName: Int, secondParameterName: Int) {
  2.         // function body goes here
  3.         // firstParameterName and secondParameterName refer to
  4.         // the argument values for the first and second parameters
  5.     }

对上面的函数定义,要用someFunction(1, secondParameterName: 2)这种方式来调用这个函数,这里面第一个参数的名字省略掉了,跟只有一个参数的函数调用方式是一样的,如果不止一个参数,则后面的参数必须加上参数的名字,也就是external parameter name,同时这个名字也是个local parameter name。所有的local name必须唯一,而external name则有可能重复。
4.可以同时指定外部名字和本地名字,比如

  1. func sayHello(to person: String, and anotherPerson: String) -> String {
  2.     return "Hello \(person) and \(anotherPerson)!"
  3. }
  4. print(sayHello(to: "Bill", and: "Ted"))
5.如果调用函数时不想写参数名字,可以这样

  1. func someFunction(firstParameterName: Int, _ secondParameterName: Int) {
  2.         // function body goes here
  3.         // firstParameterName and secondParameterName refer to
  4.         // the argument values for the first and second parameters
  5.     }
  6.  someFunction(1, 2)
6.swift为什么要这样对parameter的名字大费周章,文档里的解释我认为是bull shit,不过让我想起了fortran对参数的处理,似乎又comfortable了一点点。
7.可以有缺省值参数,跟c++句法一样。
8.
  1. func arithmeticMean(numbers: Double...) -> Double {
  2.         var total: Double = 0
  3.         for number in numbers {
  4.             total += number
  5.         }
  6.         return total / Double(numbers.count)
  7.     }
  8.     arithmeticMean(1, 2, 3, 4, 5)
  9.     // returns 3.0, which is the arithmetic mean of these five numbers
  10.     arithmeticMean(3, 8.25, 18.75)
  11.     // returns 10.0, which is the arithmetic mean of these three numbers
这里的variadic 参数numbers就等同于一个[Double],跟printf库函数以及C++里的可变参数语义是完全不同的。不要指望可以通过func arithmeticMean(numbers: Double...,characters:Character...) -> Double {}来定义有多个variadic 参数的函数,一个函数只能有一个variadic 参数。
9.之所以会出现variable参数,是因为在swift函数中缺省是不能改变参数的,或者说swift函数的参数缺省是constant的,这个constant的属性也可以用通过let修饰参数名字来强调。但是在c/c++中函数的参数是可以更改的,这种情况在swift中可以通过用var修饰参数名字来实现。
10.
  1. func swapTwoInts(inout a: Int, inout _ b: Int) {
  2.         let temporaryA = a
  3.         a = b
  4.         b = temporaryA
  5.     }



  6.     var someInt = 3
  7.     var anotherInt = 107
  8.     swapTwoInts(&someInt, &anotherInt)
  9.     print("someInt is now \(someInt), and anotherInt is now \(anotherInt)")
  10.     // prints "someInt is now 107, and anotherInt is now 3"
这段代码无需解释,这不是按名传送,不是按名传送,不是按名传送,这是引用,这是引用,这是引用。
11.In-out parameters cannot have default values, and variadic parameters cannot be marked as inout;inout,let,var是互斥的。
12.函数的类型由函数的参数类型和返回值类型组合而成。
  1. func addTwoInts(a: Int, _ b: Int) -> Int {
  2.         return a + b
  3.     }
  4.     func multiplyTwoInts(a: Int, _ b: Int) -> Int {
  5.         return a * b
  6.     }
这两个函数的类型都是(Int, Int) -> Int. 可以读作:

“A function type that has two parameters, both of type Int, and that returns a value of type Int.”


  1. func printHelloWorld() {
  2.         print("hello, world")
  3.     }
() -> Void, or “a function that has no parameters, and returns Void.”

13.
  1. var mathFunction: (Int, Int) -> Int = addTwoInts
定义了一个变量mathFunction,这个变量的类型是a function that takes two Int values, and returns an Int value.
并把这个变量设成指向前面定义的函数addTwoInts.
这样的声明方式是要比signal函数的声明容易理解得多吧。
然后
  1. print("Result: \(mathFunction(2, 3))")
  2.     // prints "Result: 5"
再然后
  1. mathFunction = multiplyTwoInts
  2.     print("Result: \(mathFunction(2, 3))")
  3.     // prints "Result: 6"
当然,对于swift来说
  1. let anotherMathFunction = addTwoInts
  2.     // anotherMathFunction is inferred to be of type (Int, Int) -> Int
也是毫无难度的。
14.
  1. func printMathResult(mathFunction: (Int, Int) -> Int, _ a: Int, _ b: Int) {
  2.         print("Result: \(mathFunction(a, b))")
  3.     }
  4.     printMathResult(addTwoInts, 3, 5)
  5.     // prints "Result: 8"
这就是我为什么会提到signal的原因。当然这是个回调。
15.也可以这样
  1. func stepForward(input: Int) -> Int {
  2.         return input + 1
  3.     }
  4.     func stepBackward(input: Int) -> Int {
  5.         return input - 1
  6.     }

  7. func chooseStepFunction(backwards: Bool) -> (Int) -> Int {
  8.         return backwards ? stepBackward : stepForward
  9.     }

  10.     var currentValue = 3
  11.     let moveNearerToZero = chooseStepFunction(currentValue > 0)
  12.     // moveNearerToZero now refers to the stepBackward() function
函数指针。





阅读(929) | 评论(0) | 转发(0) |
0

上一篇:流水灯-1

下一篇:swift笔记-Closures

给主人留下些什么吧!~~