Skip to Main Content
iOS 编程基础:Swift、Xcode 和Cocoa 入门指南
book

iOS 编程基础:Swift、Xcode 和Cocoa 入门指南

by Matt Neuburg
January 2017
Intermediate to advanced content levelIntermediate to advanced
552 pages
9h 43m
Chinese
China Machine Press
Content preview from iOS 编程基础:Swift、Xcode 和Cocoa 入门指南
函数
53
为了让函数类型说明符更加清晰,请通过
Swift
typealias
特性创建一个类型别名,
为函数类型赋予一个名字。这个名字可以是描述性的,请不要与箭头运算符符号搞
混。比如,如果定义
typealias VoidVoidFunction = () -> ()
那就可以在通过
该签名指定函数类型时使用
VoidVoidFunction
了。
2.11
匿名函数
再来看看之前的示例:
func whatToAnimate() { // self.myButton is a button in the interface
self.myButton.frame.origin.y += 20
}
func whatToDoLater(finished:Bool) {
print("finished: \(finished)")
}
UIView.animateWithDuration(
0.4, animations: whatToAnimate, completion: whatToDoLater)
上述代码有些丑陋。我声明函数
whatToAnimate
whatToDoLater
的唯
一目的就是将它
们传递给最下面一行的函数。我其实并不需要
whatToAnimate
whatToDoLater
这两个
名字,只不过就是为了在最后一行代码中引用它们而已;无论是名字还是这两个函数后
面都不会再用到了。因此,要是不需要名字而只传递这两个函数的函数体就好了。
这叫作匿名函数,在
Swift
中是合法且常见的。为了构造匿名函数,你需要完成两件事: ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

机器学习实战:基于Scikit-Learn、Keras 和TensorFlow (原书第2 版)

机器学习实战:基于Scikit-Learn、Keras 和TensorFlow (原书第2 版)

Aurélien Géron
Swift 人工智能实战:从基础理论到AI 驱动的应用程序开发

Swift 人工智能实战:从基础理论到AI 驱动的应用程序开发

Mars Geldard, Jonathon Manning, Paris Buttfield-Addison, Tim Nugent

Publisher Resources

ISBN: 9787111556350