Skip to Content
Swift学习手册
book

Swift学习手册

by Jon Manning, Paris Buttfield-Addison, Tim Nugent
February 2017
Beginner to intermediate
472 pages
9h 53m
Chinese
China Electric Power Press Ltd.
Content preview from Swift学习手册
36
2
处理错误的方式变了。现在,抛出错误的方式与异常一样,通过
do-catch
结构。详
情参阅第
3
章的“错误处理”。
do
关键字现在用于处理错误
do-catch
结构了,因此
do-while
句法改成
repeat-
while
了。
可以使用
defer
关键字编写在退出当前作用域时运行的代码。比如说我们先分配了
一个资源,然后在下一行使用
defer
,指明代码流退出当前函数时释放资源。
可以使用
guard
关键字确保某个条件是真的,不然立即退出代码流。详情参阅本章
后面的“
guard
关键字”。
2.2 Playground
学习
Swift
最简单的途径是使用
Playground
。在
Playground
环境中可以编写
Swift
代码,
而且能立即看到结果。为了查看结果,无需构建并运行代码,代码也不用是大型应用的
一部分。这意味着,如果你想试验这门语言,定义一个函数,或者执行一个大型应用的
一部分代码,无需把代码写入一个完整的应用中。
本章(不是本书)余下的内容都假定你在
Playground
中编写代码。如果你想跟着我
们一起学,应该适应这种方式。
Playground
特别有用,我们建议你使用它试验和学习
Swift
学习和使用
Swift
时,最好能快速访问一个
Playground
,我们建议你把一个
Playground
文件拖曳到
OX X Dock
中,这样便于快速测试
Swift
代码。
你可以在
Xcode
启动时显示的“
Welcome to Xcode
”界面中创建
Playground
(见图
2-1
)。
也可以选择“ ...
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.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

PyTorch深度学习

PyTorch深度学习

Posts & Telecom Press, Vishnu Subramanian
深入淺出代數

深入淺出代數

Tracey Pilone, Dan Pilone
PHP学习手册

PHP学习手册

David Sklar
C语言核心技术(原书第2版)

C语言核心技术(原书第2版)

Peter Prinz, Tony Crawford

Publisher Resources

ISBN: 9787519803223