第一章,必備編程基礎(chǔ)和背景知識(shí)----Swift Apprentice中文版

? ? ? ? 本章簡(jiǎn)介代碼的基礎(chǔ)支持弦讽,代碼運(yùn)行的基本原理和swfit編程工具寿酌。

電腦工作原理簡(jiǎn)介:

? ? ? ?其實(shí)電腦并沒(méi)有大家想象的那么強(qiáng)大拳喻,他的強(qiáng)大之處在于使用他的人径缅,通過(guò)聰明的合理的方式組織他的計(jì)算能力,從而實(shí)現(xiàn)強(qiáng)大的功能更潜圃。并且計(jì)算機(jī)也簡(jiǎn)單到爆缸棵,他的核心是cpu,cpu是用來(lái)做各種數(shù)字處理的核心單元谭期,他的主要工作就是加減乘除操作堵第,只是他的計(jì)算能力有點(diǎn)強(qiáng)大,每秒鐘能計(jì)算幾億次隧出。

CPU直接處理的數(shù)據(jù)一般是通過(guò)寄存器進(jìn)行存取踏志,這是cpu可以直接操作的器件,他的特點(diǎn)是速度快胀瞪,安全性高针余,缺點(diǎn)是容量小。當(dāng)CPU處理的數(shù)據(jù)沒(méi)有在寄存器中時(shí)凄诞,他回去RAM圆雁,就是我們常說(shuō)的內(nèi)存里面取。同時(shí)會(huì)將結(jié)果緩存的寄存器帆谍,合適的時(shí)間再放回到內(nèi)存中伪朽。原文中的圖和描述都有點(diǎn)簡(jiǎn)單,沒(méi)有考慮一級(jí)緩存汛蝙,二級(jí)緩存還有磁盤(pán)的情況烈涮,大家僅做參考朴肺。

? ? ? ? CPU不停從寄存器或者內(nèi)存讀取指令,然后做各種加減乘除運(yùn)算坚洽,最后再把結(jié)果放到寄存器和內(nèi)容中戈稿,軟件就是這些指令的集合。軟件通過(guò)這些指令可以讓大家上網(wǎng)聊天把妹子讶舰,一個(gè)軟件的指令數(shù)量是非常驚人的鞍盗。

? ? ? ? 軟件的生成需要編程語(yǔ)言和編譯工具。比如本文介紹的swift就是一種編程語(yǔ)言绘雁,通過(guò)電腦上的編譯工具橡疼,你寫(xiě)的代碼會(huì)被翻譯成CPU能夠理解并執(zhí)行的指令。一條編程語(yǔ)言的代碼可能會(huì)被編譯器翻譯成很多條指令庐舟,有的甚至可能會(huì)變成成百上千條指令欣除。

數(shù)字的表示方式:?

? ? ? ? 數(shù)字之于CPU處理器正如面包和牛奶之于我們。CPU能處理的就是數(shù)字挪略,因此我們的軟件和軟件要處理的對(duì)象全部都是數(shù)字历帚,我們看到的文本是數(shù)字,看到的圖片也是由成千上萬(wàn)的數(shù)字表示的杠娱,圖片上的每一個(gè)點(diǎn)叫做像素挽牢,一個(gè)像素有紅綠藍(lán)三色組成:純紅色就用 100%紅,0%綠摊求, 0%藍(lán)這些數(shù)字表示禽拔,每個(gè)像素都有這些數(shù)字表示,所有像素按照一定的順序保存起來(lái)就能夠精確描述一張電子照片室叉。

? ? ? ? 但是計(jì)算機(jī)中的數(shù)字表示方式與我們?nèi)粘I钪惺褂玫臄?shù)字表示方式完全不是一回事兒睹栖,在我們生活中,我們使用的都是10進(jìn)制的表示方式茧痕,比如432就是表示4百3十2:

每一位數(shù)字都是后面數(shù)字的10倍野来,這是我們通常生活中使用數(shù)字的方式:

? ? ? ? ? ? ?(0*1000) + (4*100) + (2*10) + (3*1) =423

二進(jìn)制數(shù)字:

對(duì)應(yīng)于10進(jìn)制數(shù)字表達(dá)方式,2進(jìn)制數(shù)字是以2為基礎(chǔ):


? ? ? ? (1*8) + (1*4) + (0*2) + (1*1) =13

? ? ? ? 其中8= 2的3次方 4=2的2次方踪旷, 2 = 2的一次方曼氛, 1 = 2的0次方,對(duì)應(yīng)于十進(jìn)制的表達(dá)方式中令野,1000 = 10的3次方舀患, 100 = 10的2次方, 10 = 10的一次方 1 = 10的0次方气破。通過(guò)此對(duì)應(yīng)關(guān)系构舟,你就可以理解什么是10進(jìn)制表示,什么是2進(jìn)制表示。

位數(shù)

? ? ? ? ?計(jì)算機(jī)除了表示數(shù)字的方式和日常生活不一樣外狗超,它能夠表示的數(shù)字的長(zhǎng)度也是有限制的,比如上圖中朴下,為了表示13 這個(gè)數(shù)字努咐,我們使用4個(gè)位表示,第一個(gè)權(quán)重是8的位殴胧,值是1渗稍,第二個(gè)權(quán)重是4的值是1 ,第三個(gè)權(quán)重是2的值是0团滥, 最后一個(gè)權(quán)重是1竿屹,值也是1.這個(gè)4個(gè)位我們就說(shuō)他是4個(gè)二進(jìn)制位,或者4個(gè)比特位灸姊。因?yàn)橛?jì)算機(jī)內(nèi)存的限制拱燃,所有數(shù)字的表示全都用統(tǒng)一的位數(shù)長(zhǎng)度,32比特位或者64比特位力惯。

16進(jìn)制:

? ? ? ? 與2進(jìn)制相同碗誉,16進(jìn)制表示的每一位的權(quán)重是16,但是因?yàn)槲覀內(nèi)粘I钪惺褂玫陌⒗當(dāng)?shù)字一位數(shù)字最大是9父晶,所以計(jì)算機(jī)系統(tǒng)是一個(gè)字母來(lái)表示超出9的值:

? ? ? ? ?a=10? ? ?b=11? ? ?c=12? ? ?d=13? ? ?e=14? ? ?f=15

因此他每一位的權(quán)重就會(huì)比較大哮缺,類(lèi)似于2進(jìn)制的表示,16進(jìn)制的表示如上圖:

4096 = 16的3次方 甲喝, 256=16的2次方尝苇,? 16 = 16的1次方, 1 = 16的0次方埠胖。

上面數(shù)字換算成10進(jìn)制的數(shù)值應(yīng)該是:

(12*4096) + (0*256) + (13*16) + (14*1) =49374

code的工作原理:

計(jì)算機(jī)是大廚糠溜,code就是菜譜,而數(shù)據(jù)就是食材押袍。下面以打印圖片為例:

第一步:從磁盤(pán)價(jià)值圖片

第二步: 調(diào)整圖片的大小為400*300

第三步:在圖片上加點(diǎn)點(diǎn)綴

第四步:打印圖片诵冒。

? ? ? ? ? 以上就是一種code,就是一種程序谊惭,一堆動(dòng)作列表汽馋,只是上面的code無(wú)法直接編譯執(zhí)行,因?yàn)檫€沒(méi)有這樣的編譯器可以理解上面這段指令的意義圈盔,不過(guò)隨著人工智能的發(fā)展豹芯,以后會(huì)出現(xiàn)這樣的編程方式,就是輸入一堆介于人類(lèi)語(yǔ)言和計(jì)算機(jī)執(zhí)行程序之間的code驱敲,然后人工智能機(jī)器人就會(huì)將上面的一堆動(dòng)作列表轉(zhuǎn)換為計(jì)算機(jī)可以理解并執(zhí)行的指令铁蹈。

? ? ? ? 顯然現(xiàn)在還沒(méi)有出現(xiàn)這種編程方式,而swift也不具備這樣的能力众眨。但是swift已經(jīng)汲取以前古老語(yǔ)言的優(yōu)勢(shì)握牧,并避免了舊的編程語(yǔ)言的缺點(diǎn)容诬。相對(duì)來(lái)說(shuō),swift是相對(duì)比較高級(jí)和現(xiàn)代的編程方式沿腰。最近在看資料的時(shí)候览徒,發(fā)現(xiàn)大部分demo和程序都是用swift編寫(xiě),因此這是我為什么會(huì)翻譯這本書(shū)的原因颂龙,編程語(yǔ)言在更新习蓬,我們編程人員也需要跟新只是。就像我說(shuō)的措嵌,有一天人工智能會(huì)理解近似人類(lèi)自然語(yǔ)言的編程代碼躲叼,那個(gè)時(shí)候,swift也會(huì)成為我們所說(shuō)的古老語(yǔ)言企巢,而我們大多數(shù)人只需要簡(jiǎn)單的學(xué)習(xí)就能夠?qū)崿F(xiàn)輕松的編程枫慷。彼時(shí)我們這樣的程序員就會(huì)像是現(xiàn)在的補(bǔ)鞋補(bǔ)鍋匠,雖然也是一門(mén)技術(shù)包斑,但用途不多了流礁。

開(kāi)發(fā)工具----Playground

? ? ? ?編寫(xiě)code的工具一般叫做IDE(集成開(kāi)發(fā)工具),這個(gè)工具會(huì)有一整套的軟件罗丰,將你編寫(xiě)的代碼轉(zhuǎn)換為可以執(zhí)行的神帅,計(jì)算機(jī)能理解的指令集合,就是軟件萌抵。

? ? ? ? swift的編程工具之一是XCode找御,這個(gè)IDE有一個(gè)Playground功能,可以在不用編譯的情況下產(chǎn)看代碼執(zhí)行結(jié)果绍填,我們之前講過(guò)霎桅,代碼需要編譯器解釋成CPU理解的質(zhì)量才能夠執(zhí)行,而這個(gè)地方所謂的無(wú)需編譯就能夠查看代碼又是什么呢讨永?其實(shí)在高級(jí)編程語(yǔ)言中滔驶,沒(méi)有那種代碼不需要解釋編譯就能夠執(zhí)行,這里所謂的需要編譯卿闹,只是xcode做了很多實(shí)時(shí)編譯和解釋?zhuān)屇銦o(wú)需單獨(dú)再執(zhí)行一遍編譯過(guò)程而已揭糕。

創(chuàng)建一個(gè)Playground:

創(chuàng)建之前,你首先要下載Xcode這個(gè)IDE锻霎。打開(kāi)XCODE之后你應(yīng)該能夠看到這個(gè)界面著角。

如果沒(méi)有顯示,說(shuō)明你在設(shè)置中將這個(gè)界面修改為啟動(dòng)不顯示了旋恼。產(chǎn)看這個(gè)界面的辦法是:Command-Shift-1

或者是 菜單欄中Window-->Welcome to Xcode.

在這個(gè)界面中吏口,從左邊選擇"Get started with a playground",就是圖中第一個(gè)選項(xiàng)。接著會(huì)顯示新界面:

按照如圖選擇,然后next會(huì)提示你工程的保存路徑和工程名产徊。設(shè)置完畢之后顯示下面的界面

區(qū)域1-代碼區(qū):這就是我們需要編寫(xiě)swift代碼的地方

區(qū)域2-結(jié)果區(qū):代碼運(yùn)行結(jié)果的顯示的區(qū)域

區(qū)域3-是否是自動(dòng)編譯運(yùn)行的按鈕開(kāi)關(guān)昂勒,如果打開(kāi),區(qū)域2會(huì)自動(dòng)執(zhí)行區(qū)域1的代碼舟铜。

區(qū)域4-自動(dòng)運(yùn)行狀態(tài)叁怪,如果代碼還在編譯執(zhí)行,那這個(gè)地方會(huì)有等待提示深滚。

區(qū)域5-面板顯示控制,這個(gè)按鈕可以控制左邊涣觉,下面痴荐,和右邊的三個(gè)面板是否顯示。你可以選擇點(diǎn)擊一下官册,看看這三個(gè)面板分別顯示設(shè)什么內(nèi)容生兆。

如果你的XCode的代碼區(qū)沒(méi)有顯示代碼的行號(hào),你可以通過(guò)這個(gè)地方進(jìn)行設(shè)置膝宁。Xcode->

小結(jié):

計(jì)算機(jī)的本身只能做簡(jiǎn)單的數(shù)學(xué)運(yùn)算

編程語(yǔ)言允許你編寫(xiě)代碼鸦难,然后代碼生成計(jì)算機(jī)可以理解并執(zhí)行的程序

CPU處理的是2進(jìn)制的數(shù)據(jù)

我們學(xué)習(xí)Swift的開(kāi)發(fā)工具是XCode

Playground這個(gè)功能可以讓我們快速查看代碼的執(zhí)行效果。



Welcome to our book! In this first chapter, you’re going to learn a few basics. You’ll

learn how the code works first. Then you’ll learn about the tools you’ll be using to write

Swift code.

How a computer works

You may not believe me when I say it, but a computer is not very smart on its own.

The power of computers is all derived from how they’re programmed by people like

you and me. If you want to successfully harness the power of a computer — and I

assume you do if you’re reading this book — it’s important to understand how

computers work.

It may also surprise you to learn that computers themselves are rather simple

machines. At the heart of a computer is a Central Processing Unit(CPU). This is

essentially a math machine. It performs addition, subtraction, and other

arithmetical operations on numbers. Everything you see when you operate your

computer is all built upon a CPU crunching numbers many millions of times per

second. Isn’t it amazing what can come from just numbers?



The CPU stores the numbers it acts upon in small memory units called registers.

The CPU is able to read numbers into registers from the computer’s main memory,

known as Random Access Memory(RAM). It’s also able to write the number

stored in a register back into RAM. This allows the CPU to work with large amounts

of data that wouldn’t all fit in the bank of registers.

Here is a diagram of how this works:



As the CPU pulls values from RAM into its registers, it uses those values in its math

unit and stores the results back in another register.

Each time the CPU makes an addition, a subtraction, a read from RAM or a write to

RAM, it’s executing a single instruction. Each computer program is usually made

up of thousands to millions of instructions. A complex computer program such as

your operating system, macOS (yes, that’s a computer program too!), may have

many millions of instructions in total.

It’s entirely possible to write individual instructions to tell a computer what to do,

but for all but the simplest programs, it would be immensely time-consuming and

tedious. This is because most computer programs aim to do much more than simple

math — computer programs let you surf the Internet, manipulate images, and allow

you to chat with your friends.

Instead of writing individual instructions, you write code in a specific programming language, which in your case will be Swift. This code is put through

a computer program called a compiler, which converts the code into instructions

the CPU knows how to execute. Each line of code you write will turn into many

instructions — some lines could end up being tens of instructions!

Representing numbers

As you know by now, numbers are a computer’s bread and butter, the fundamental

basis of everything it does. Whatever information you send to the compiler will

eventually become a number. For example, each character within a block of text is

represented by a number. You’ll learn more about this in Chapter 3, which delves

into types including strings, the computer term for a block of text.

Images are no exception. In a computer, each image is also represented by a series

of numbers. An image is split into many thousands, or even millions, of picture

elements called pixels, where each pixel is a solid color. If you look closely at your

computer screen, you may be able to make out these blocks. That is unless you

have a particularly high-resolution display where the pixels are incredibly small!

Each of these solid color pixels is usually represented by three numbers: one for the

amount of red, one for the amount of green and one for the amount of blue. For

example, an entirely red pixel would be 100% red, 0% green and 0% blue.

The numbers the CPU works with are notably different from those you are used to.

When you deal with numbers in day-to-day life, you work with them in base 10,

otherwise known as the decimal system. Having used this numerical system for so

long, you intuitively understand how it works. So that you can you can appreciate

the CPU’s point of view, consider how base 10 works.

The decimal or base 10 number 423 contains three units, two tens and four

hundreds:


In the base 10 system, each digit of a number can have a value of 0, 1, 2, 3, 4, 5,

6, 7, 8 or 9, giving a total of 10 possible values for each digit. Yep, that’s why it’s

called base 10!

But the true value of each digit depends on its position within the number. Moving

from right to left, each digit gets multiplied by an increasing power of 10. So the

multiplier for the far-right position is 10 to the power of 0, which is 1. Moving to the

left, the next multiplier is 10 to the power of 1, which is 10. Moving again to the

left, the next multiplier is 10 to the power of 2, which is 100. And so on.

This means each digit has a value ten times that of the digit to its right. The

number423is equal to the following:

(0*1000) + (4*100) + (2*10) + (3*1) =423Binary numbers

Because you’ve been trained to operate in base 10, you don’t have to think about

how to read most numbers — it feels quite natural. But to a computer, base 10 is

way too complicated! Computers are simple-minded, remember? They like to work

with base 2.

Base 2is often called binary, which you’ve likely heard of before. It follows that

base 2 has only two options for each digit: 0 or 1.

Almost all modern computers use binary because at the physical level, it’s easiest

to handle only two options for each digit. In digital electronic circuitry, which is

mostly what comprises a computer, the presence of an electrical voltage is 1 and

the absence is 0 — that’s base 2!

Here’s a representation of the base 2 number 1101:


In the base 10 number system, the place values increase by a factor of 10: 1, 10,

100, 1000, etc. In base 2, they increase by a factor of 2: 1, 2, 4, 8, 16, etc. The

general rule is to multiply each digit by an increasing power of the base number —

in this case, powers of 2 — moving from right to left.

So the far-right digit represents (1 * 2^0), which is (1 * 1), which is 1. The next

digit to the left represents (0 * 2^1), which is (0 * 2), which is 0. In the illustration

above, you can see the powers of 2 on top of the blocks.

Put another way, every power of 2 either is (1) or isn’t (0) present as a component

of a binary number. The decimal version of a binary number is the sum of all the

powers of 2 that make up that number. So the binary number 1101 is equal to:

(1*8) + (1*4) + (0*2) + (1*1) =13

And if you wanted to convert the base 10 number 423 into binary, you would simply

need to break down 423 into its component powers of 2. You would wind up with

the following:

As you can see by scanning the binary digits in the above equation, the resulting

binary number is 110100111. You can prove to yourself that this is equal to 423 by

doing the math!

The computer term given to each digit of a binary number is abit(a contraction of

“binary digit”). Eight bits make up abyte. Four bits is called anibble, a play on

words that shows even old school computer scientists had a sense of humor.


A computer’s limited memory means it can normally deal with numbers up to a

certain length. Each register, for example, is usually 32 or 64 bits in length, which is

why we speak of 32-bit and 64-bit CPUs.

Therefore, a 32-bit CPU can handle a maximum base-number of 4,294,967,295,

which is the base 2 number11111111111111111111111111111111. That is 32 ones—

count them!

It’s possible for a computer to handle numbers that are larger than the CPU

maximum, but the calculations have to be split up and managed in a special and

longer way, much like the long multiplication you performed in school.

Hexadecimal numbers

As you can imagine, working with binary numbers can become quite tedious,

because it can take a long time to write or type them. For this reason, in computer

programming, we often use another number format known ashexadecimal, orhexfor short. This isbase 16.

Of course, there aren’t 16 distinct numbers to use for digits; there are only 10. To

supplement these, we use the first six letters,athroughf. They are equivalent to

decimal numbers like so:

?a=10

?b=11

?c=12

?d=13

?e=14

?f=15

Here’s a base 16 example using the same format as before:


Notice first that you can make hexadecimal numbers look like words. That means

you can have a little bit of fun. :]

Now the values of each digit refer to powers of 16. In the same way as before, you

can convert this number to decimal like so:

(12*4096) + (0*256) + (13*16) + (14*1) =49374

You translate the letters to their decimal equivalents and then perform the usual

calculations.

But why bother with this?

Hexadecimal is important because each hexadecimal digit can represent precisely

four binary digits. The binary number1111is equivalent to hexadecimalf. It follows

that you can simply concatenate the binary digits representing each hexadecimal

digit, creating a hexadecimal number that is shorter than its binary or decimal

equivalents.

For example, consider the numberc0defrom above:

This turns out to be rather helpful, given how computers use long 32-bit or 64-bit

binary numbers. Recall that the longest 32-bit number in decimal is 4,294,967,295.

In hexadecimal, it isffffffff. That’s much more compact and clear.

How code works

Computers have a lot of constraints, and by themselves, they can only do a small

number of things. The power that the computer programmer adds, through coding,

is putting these small things together, in the right order, to produce something

much bigger.

Coding is much like writing a recipe. You assemble ingredients (the data) and give

the computer a step-by-step recipe for how to use them.

Here’s an example:

c = 1100

0 = 0000

d = 1101

e = 1110

c0de = 1100 0000 1101 1110

Step 1. Load photo from hard drive.

Step 2. Resize photo to 400 pixels wide by 300 pixels high.

Step 3. Apply sepia filter to photo.

Step 4. Print photo.

This is what’s known aspseudo-code. It isn’t written in a valid computer

programming language, but it represents thealgorithmthat you want to use. In

this case, the algorithm takes a photo, resizes it, applies a filter and then prints it.

It’s a relatively straightforward algorithm, but it’s an algorithm nonetheless!


Swift code is just like this: a step-by-step list of instructions for the computer.

These instructions will get more complex as you read through this book, but the

principle is the same: You are simply telling the computer what to do, one step at a

time.

Each programming language is a high-level, pre-defined way of expressing these

steps. The compiler knows how to interpret the code you write and convert it into

instructions that the CPU can execute.

There are many different programming languages, each with its own advantages

and disadvantages. Swift is an extremely modern language. It incorporates the

strengths of many other languages while ironing out some of their weaknesses. In

years to come, we’ll look back on Swift as being old and crusty, too. But for now,

it’s an extremely exciting language because it is quickly evolving.

This has been a brief tour of computer hardware, number representation and code,

and how they all work together to create a modern program. That was a lot to

cover in one section! Now it’s time to learn about the tools you’ll use to write in

Swift as you follow along with this book.

Playgrounds

The set of tools you use to write software is often referred to as thetoolchain. The

part of the toolchain into which you write your code is known as theIntegrated

Development Environment(IDE). The most commonly used IDE for Swift is

called Xcode, and that’s what you’ll be using.

Xcode includes a handy feature called aPlayground, which allows you to quickly

write and test code without needing to build a complete app. You’ll use playgrounds

throughout the book to practice coding, so it’s important to understand how they

work. That’s what you’ll learn during the rest of this chapter.

Creating a playground

When you open Xcode, it will greet you with the following welcome screen:


If you don’t see this screen, it’s most likely because the “Show this window when

Xcode launches” option was unchecked. You can also open the screen by pressingCommand-Shift-1or clickingWindow\Welcome to Xcodefrom the menu bar.

From the welcome screen, you can jump quickly into a playground by clicking onGet started with a playground. Click on that now and Xcode will take you to a

new screen:


From here, you can name the playground and select your desired platform. The

name is merely cosmetic and for your own use; when you create your playgrounds,

feel free to choose names that will help you remember what they’re about. For

example, while you’re working through Chapter 2, you may want to name your

playgroundChapter2.

The second option you can see here is the platform. Currently, this can be eitheriOS,macOSortvOS.

The platform you choose simply defines which template Xcode will use to create the

playground. Each platform comes with its own environment set up and ready for

you to begin playing around with code. For the purposes of this book, choose

whichever you wish. You won’t be writing any platform-specific code; instead, you’ll

be learning the core principles of the Swift language.

Once you’ve chosen a name and a platform, click onNextand then save the

playground. Xcode then presents you with the playground, like so:


New playgrounds don’t start entirely empty but have some basic starter code to get

you going. Don’t worry — you’ll soon learn what this code means.

Playgrounds overview

At first glance, a playground may look like a rather fancy text editor. Well, here’s

some news for you: It is essentially just that!


The above screenshot highlights the first and most important things to know about:

1.Sourceeditor:Thisistheareainwhichyou’llwriteyourSwiftcode.It’smuch

like a text editor such as Notepad or TextEdit. You’ll notice the use of what’s

known as a monospace font, meaning all characters are the same width. This

makes the code much easier to read and format.

2.Resultssidebar:Thisareashowstheresultsofyourcode.You’lllearnmore

about how code is executed as you read through the book. The results sidebar

will be the main place you’ll look to confirm your code is working as expected.

3.Executioncontrol:Playgroundsexecuteautomaticallybydefault,meaningyou

can write code and immediately see the output. This control allows you to

execute the playground again. Holding down the button allows you to switch

between automatic execution and manual execution modes.

4.Activityviewer:Thisshowsthestatusoftheplayground.Inthescreenshot,it

shows that the playground has finished executing and is ready to handle more

code in the source editor. When the playground is executing, this viewer will

indicate this with a spinner.

5.Panel controls: These toggle switches show and hide three panels,one that

appears on the left, one on the bottom and one on the right. The panels each

display extra information that you may need to access from time to time. You’ll

usually keep them hidden, as they are in the screenshot. You’ll learn more

about each of these panels as you move through the book.

Playgrounds execute the code in the source editor from top to bottom. Every time

you change the code, the playground will re-execute everything. You can also force

a re-execution by clickingEditor\Execute Playground. Alternatively, you can use

the execution control.

You can turn on line numbers on the left side of the source editor by clickingXcode

\Preferences...\Text Editing\Line Numbers. Line numbers can be very useful

when you want to refer to parts of your code.

Once the playground execution is finished, Xcode updates the results sidebar to

show the results of the corresponding line in the source editor. You’ll see how to

interpret the results of your code as you work through the examples in this book.

Key points

Computers, at their most fundamental level, perform simple mathematics.

A programming language allows you to write code, which the compiler converts

into instructions that the CPU can execute.

Computers operate on numbers in base 2 form, otherwise known as binary.

The IDE you use to write Swift code is named Xcode.

By providing immediate feedback about how code is executing, playgrounds allow

you to write and test Swift code quickly and efficiently.

Where to go from here?

If you haven’t done so already, open Xcode for yourself and create your first

playground. Give it the nameChapter01and use the iOS platform. Save it

somewhere on your hard drive and you’ll end up with an open playground.

Now you’re all set to follow along with the next chapter!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末员淫,一起剝皮案震驚了整個(gè)濱河市合蔽,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌介返,老刑警劉巖拴事,帶你破解...
    沈念sama閱讀 207,248評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異圣蝎,居然都是意外死亡刃宵,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,681評(píng)論 2 381
  • 文/潘曉璐 我一進(jìn)店門(mén)徘公,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)牲证,“玉大人,你說(shuō)我怎么就攤上這事关面√古郏” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 153,443評(píng)論 0 344
  • 文/不壞的土叔 我叫張陵缭裆,是天一觀的道長(zhǎng)键闺。 經(jīng)常有香客問(wèn)我,道長(zhǎng)澈驼,這世上最難降的妖魔是什么辛燥? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 55,475評(píng)論 1 279
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上挎塌,老公的妹妹穿的比我還像新娘徘六。我一直安慰自己,他們只是感情好榴都,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,458評(píng)論 5 374
  • 文/花漫 我一把揭開(kāi)白布待锈。 她就那樣靜靜地躺著,像睡著了一般嘴高。 火紅的嫁衣襯著肌膚如雪竿音。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 49,185評(píng)論 1 284
  • 那天拴驮,我揣著相機(jī)與錄音春瞬,去河邊找鬼。 笑死套啤,一個(gè)胖子當(dāng)著我的面吹牛宽气,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播潜沦,決...
    沈念sama閱讀 38,451評(píng)論 3 401
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼萄涯,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了唆鸡?” 一聲冷哼從身側(cè)響起涝影,我...
    開(kāi)封第一講書(shū)人閱讀 37,112評(píng)論 0 261
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎喇闸,沒(méi)想到半個(gè)月后袄琳,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,609評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡燃乍,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,083評(píng)論 2 325
  • 正文 我和宋清朗相戀三年唆樊,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片刻蟹。...
    茶點(diǎn)故事閱讀 38,163評(píng)論 1 334
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡逗旁,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出舆瘪,到底是詐尸還是另有隱情片效,我是刑警寧澤,帶...
    沈念sama閱讀 33,803評(píng)論 4 323
  • 正文 年R本政府宣布英古,位于F島的核電站淀衣,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏召调。R本人自食惡果不足惜膨桥,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,357評(píng)論 3 307
  • 文/蒙蒙 一蛮浑、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧只嚣,春花似錦沮稚、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,357評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至调鲸,卻和暖如春盛杰,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背藐石。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,590評(píng)論 1 261
  • 我被黑心中介騙來(lái)泰國(guó)打工饶唤, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人贯钩。 一個(gè)月前我還...
    沈念sama閱讀 45,636評(píng)論 2 355
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像办素,于是被迫代替她去往敵國(guó)和親角雷。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,925評(píng)論 2 344

推薦閱讀更多精彩內(nèi)容