Swift 中的函数(下)

学习来自《极客学院:Swift中的函数》

工具:Xcode6.4

直接上基础的示例代码,多敲多体会就会有收获:百看不如一敲,一敲就会

 1 import Foundation
 2
 3 /***********嵌套函数*************/
 4 func getMathFunc(#type: String) -> (Int) -> Int{
 5     func squre(num:Int) -> Int{
 6         return num*num
 7     }
 8     func cube(num:Int) -> Int{
 9         return num*num*num
10     }
11     switch(type){
12         case "square":
13         return squre
14     default:
15         return cube
16     }
17 }
18 var mathfunc = getMathFunc(type: "other")
19 println(mathfunc(4))
20 //由于嵌套函数在外函数内部只作用于该函数内部,所以使用闭包简化这个嵌套函数
21
22 /***********闭包*************/
23 /*
24 { (形参列表) -> (返回值) in
25     可执行语句
26 }
27 */
28 func getMathFunc_1(#type: String) -> (Int) -> Int{
29 //    func squre(num:Int) -> Int{
30 //        return num*num
31 //    }
32 //    func cube(num:Int) -> Int{
33 //        return num*num*num
34 //    }
35     switch(type){
36     case "square":
37         return {
38             (num:Int) -> Int in
39             return num * num
40         }
41     default:
42         return {
43             (num:Int) ->Int in
44             return num * num * num
45         }
46     }
47 }
48 var mathfunc_1 = getMathFunc_1(type: "other")
49 println(mathfunc_1(5))
50 //利用上下文推断类型
51 var squre1: (Int) -> Int = {(num) in return num*num }
52 println(squre1(8))
53 var squre2: (Int) -> Int = {num in return num*num }
54 println(squre2(9))
55 //省略形参名,通过$0,$1...来引用第一个、第二个参数
56 var squre3: (Int) -> Int = {$0 * $0}
57 println(squre3(10))
58
59 //闭包的经典使用
60 var result: Int = {
61     var result = 1
62     for i in 1...$1{
63         result *= $0
64     }
65     return result
66 }(4,3)
67 println(result)
68
69 //尾随闭包
70 func someFunction(num:Int,fn: (Int)->()){
71     //执行代码
72 }//第一个参数为整型,第二个参数为闭包
73 //调用这个函数: someFunction(20,{})
74 //使用尾随闭包调用函数的格式:someFunction(20){}
75
76 //捕获上下文中的变量或者常量
77 func makeArr(ele:String) ->() ->[String]{
78     //创建一个不包含任何元素的数组
79     var arr: [String] = []
80     func addElement() -> [String]{
81         //向arr数组中添加一个元素
82         arr.append(ele)
83         return arr
84     }
85     return addElement
86 }
时间: 2024-08-29 14:58:47

Swift 中的函数(下)的相关文章

浅谈swift中的函数类型和闭包

在讲swift的函数类型之前,我们先回忆一下我们以前学的定义一个swift的函数 func add(a: Int,b: Int) -> Int { return a + b } 好了, 我们开始我们函数类型的讲解 上面这个函数的类型是(Int ,Int)->Int 使用函数类型 我们都知道, 在swift中 , 函数类型就像其他数据类型一样,也就意味着我们可以给一个函数的常量或者是变量赋值 var f2: (Int,Int)-> Int = add f2(2,3) //结果为5 好了,接

Swift 中的函数(中)

学习来自<极客学院:Swift中的函数> 工具:Xcode6.4 直接上基础的示例代码,多敲多体会就会有收获:百看不如一敲,一敲就会 1 import Foundation 2 3 //函数 4 5 //1.多个返回值 6 func area(width: Double,height: Double) -> (Double,Double) 7 { 8 var b = width 9 var a = width * height 10 return (a,b) 11 } 12 printl

Swift 中的函数(上)

学习来自<极客学院:Swift中的字典> 工具:Xcode6.4 直接上基础的示例代码,多敲多体会就会有收获:百看不如一敲,一敲就会 1 import Foundation 2 3 //函数 4 /*1.定义函数的语法格式 5 func 函数名(形参列表) -> 返回值类型 { 6 //可执行语句组成的函数 7 } 8 */ 9 //定义和调用函数 10 func sayHello(personName: String) -> String{ 11 return "Hel

Swift 中调试状态下打印日志

首先我们应该知道Swift中真个程序的入口就是在AppDelegate.swift中.所以在打印日志在 AppDelegate.swift中是这样的 import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(application: UIApplication, didFinishLaunchingWi

【iOS】Swift中的函数、断言、枚举等

函数与方法.枚举等的书写格式与用法较OC还是变化很大的. 一.函数 1.1 无返回值, 无形参 func myTest2(){ println("Hello") } 1.2 有返回值,有形参 func myTest1( num1:Int , num2:Int ) -> String{ return String(num1 + num2) } 1.3 外部参数. (相当于给参数起了个别名,方便识别记忆) func myTest3 (myNum1 num1 :Int ,myNum2

Swift 中的利刃,函数和闭包

input[type="date"].form-control,.input-group-sm>input[type="date"].input-group-addon,.input-group-sm>.input-group-btn>input[type="date"].btn,input[type="time"].input-sm,.form-horizontal .form-group-sm input

Swift 中的高阶函数和函数嵌套

高阶函数 在Swift中,函数可做为"一等公民"的存在,也就意味着,我们可以和使用 int 以及 String 一样,将函数当做 参数.值.类型来使用. 其中,将函数当作一个参数和值来使用可见下: typealias addTwoInts = (Int,Int)->(Int) var funcType = addTwoInts.self func aAddb(a:Int,b:Int) -> Int { return a+b } func addFunc(_ add:addT

Swift新手教程系列5-函数+selector在swift中的使用方法

原创blog.转载请注明出处 近期在用swift写代码,尽管遇到一些问题,可是代码量确实减了不少. swfit新手教程系列会随着我使用swfit中的积累,不断地去修正更新 之前的教程 swift单例模式具体解释-线程安全,多核性能 swift新手教程4-集合(Array,Dictionary) swift新手教程3-字符串String swift新手教程2-运算符 swift新手教程1-准备知识 在swift中,函数有keywordfunc声明 格式 func 函数名(參数1,參数2,...)-

[Swift]Day06:函数

函数 参数 外部变量名 一般情况下你可以不指定外部变量名,直接调用函数: func helloWithName(name: String, age: Int, location: String) { println("Hello \(name). I live in \(location) too. When is your \(age + 1)th birthday?") } helloWithName("Mr. Roboto", 5, "San Fra