A Swift Tour(3) - Functions and Closures
Functions and Closures
使用func来声明函数,通过括号参数列表的方式来调用函数,用 --> 来分割函数的返回类型,参数名和类型,例如:
func greet(name: String, day: String) -> String {
return "Hello \(name), today is \(day)."
} greet("Bob", day: "Tuesday") //这是swift文档中的调用方法,但是我在xcode6中编写的时候总报错,所以采用了下面的方式 greet("Bob", day: "Tuesday") //使用这种方式不会错误
使用一个元组一个函数可以返回多个值
func getGasPrices() -> (Double,Double,Double)
{
return (3.97,3.59,3.79)
}
getGasPrices()
上面的方法我不知道用什么来接收返回的值,请高手支招
func 的参数也是可变的,可以把多个参数放在一个数组中
func sumOf(sumbers:Int...) -> Int
{
var sum =
for number in sumbers
{
sum += number
}
return sum
} println(sumOf()) //return 0
println(sumOf(, , )) //return 651
函数可以嵌套,嵌套的函数可以访问在外部函数中声明的变量,你可以使用嵌套函数来解决复杂的逻辑:
func returnFifteen() -> Int
{
var y =
func add()
{
y +=
}
add()
return y;
} println(returnFifteen()) //return 15
函数是一个 first-class 类型,这意味着函数的返回值可以是另一个函数:
func makeIncrementer() -> (Int -> Int) {
func addOne(number: Int) -> Int {
return + number
}
return addOne
} var increment = makeIncrementer()
increment() //上面的代码一直报错,不知道什么原因
//错误:Command /Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift failed with exit code 254 //如果哪位高手知道,请指教
一个函数可以作为另一个函数作为其参数
func hasAnyMatches(list: Int[],condition: Int-> Bool) -> Bool
{
for item in list
{
if(condition(item))
{
return true
}
}
return false
} func lessThanTen(number: Int) -> Bool
{
return number < ;
} var numbers = [,,,]
let temp = hasAnyMatches(numbers, lessThanTen)
println(temp) //这个和上面一样的错,也有事我的xcode6 有问题
你可以通过{}来些一个闭包
numbers.map({
(number: Int) -> Int in
let result = * number
return result
})
这个闭包有的时候可以写的更加简洁,比如你知道他的返回类型或者其他的
numbers.map({ number in * number })
sort([, , , , ]) { $ > $ }
上面的闭包 没搞明白。。。
A Swift Tour(3) - Functions and Closures的更多相关文章
- 【读书笔记】A Swift Tour
素材:A Swift Tour 推荐下载Playground:Download Playground objc 自己较为熟悉,想熟悉下风头正劲的 swift.就先从官方的入门手册开始撸. 每一小节,我 ...
- Swift学习——A Swift Tour 函数
Functions and Closures 函数和封闭性(闭包) Functions 函数的使用 Swift中的函数定义和OC中有明显的差别了,使用func定义函数,在括号里定义參数和类型,用 ...
- functions and closures are reference types-函数和闭包是引用类型
Closures Are Reference Types In the example above, incrementBySeven and incrementByTen are constants ...
- Swift Tour 随笔总结 (4)
Switch的一个例子: let vegetable = "red pepper" switch vegetable { case "celery": let ...
- The Swift Programming Language-官方教程精译Swift(8)闭包 -- Closures
闭包是功能性自包含模块,可以在代码中被传递和使用. Swift 中的闭包与 C 和 Objective-C中的 blocks 以及其他一些编程语言中的 lambdas 比较相似. 闭包可以捕获和存储其 ...
- A Tour of Go Function closures
Go functions may be closures. A closure is a function value that references variables from outside i ...
- 冷市攻略:Listo 教你 25 今天的社会 Swift 语言 - 02 Swift Tour
import Foundation //******************************************************************************** ...
- 【Swift】 - 函数(Functions)总结 - 比较 与 C# 的异同
1.0 函数的定义与调用( Defining and Calling Functions ) 习惯了C#了语法,看到下面的这样定义输入参数实在感到非常别扭,func 有点 Javascript的感觉, ...
- [IOS]《A Swift Tour》翻译(一)
以下翻译内容为原创,转载请注明: 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/3768936.html 碎碎念... Swift是苹果在WWDC刚发 ...
随机推荐
- spring-- 事务--9
9.1 数据库事务概述 事务首先是一系列操作组成的工作单元,该工作单元内的操作是不可分割的,即要么所有操作都做,要么所有操作都不做,这就是事务. 事务必需满足ACID(原子性.一致性.隔离性和持久性 ...
- 自动化测试实施的几个idea
UI检查.测试的一个idea 在电子商务网站中, 为达到较好的用户体验, 可能页面上会有大量的UI设计,一堆css.ajax效果等,敏捷开发中, UI变动更是带来了测试的苦恼.对于回归组catch U ...
- [POJ3684]Physics Experiment
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1363 Accepted: 476 Special Judge ...
- 【CSS3】Advanced5:At Rules:@import, @media, and @font-face
1.@import bolt another stylesheet onto your existing one. @import url(**.css); must be placed at the ...
- bzoj 2285 [Sdoi2011]保密(二分,spfa + 最大流)
Description 现在,保密成为一个很重要也很困难的问题.如果没有做好,后果是严重的.比如,有个人没有自己去修电脑,又没有拆硬盘,后来的事大家都知道了. 当然,对保密最需求的当然是军方,其次才是 ...
- HW3.23
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- HW2.11
控制台: import java.util.Scanner; public class Solution { public static void main(String[] args) { Scan ...
- hdoj 1898 Sempr == The Best Problem Solver?
Sempr == The Best Problem Solver? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/3276 ...
- oracle数据库自动备份脚本
::通过exp命令导出远程机器(192.168.2.1)上指定服务(orcl)指定用户(pmis)及密码(pmis)的数据 ::运行该脚本的机器必须安装oracle @echo off @echo [ ...
- CSS 选择器及其优先级
CSS 的选择器有很多类型,我们将常用的这些列表如下: 一.CSS 选择器的类别 1. 基本选择器 基本选择器 解释 备注 * 通用选择器,匹配所有元素 CSS2 E 元素选择器,匹配类型为 E 的所 ...