官术网_书友最值得收藏!

  • Mastering Swift
  • Jon Hoffman
  • 343字
  • 2021-07-16 14:12:15

What we have learned so far

Up to this point, we have been laying the foundation for writing applications with Swift. While it is possible to write a very basic application with what we have learned so far, it would be really difficult to write a useful application using only what we covered in the first three chapters.

Starting with this chapter, we will begin to move away from the foundations of the Swift language, and begin to learn the building blocks of application development with Swift. In this chapter, we will go over control flow and functions. To become a master of the Swift programming language, it is important that you fully understand and comprehend the concepts discussed in this chapter and in Chapter 5, Classes and Structures.

Before we cover control flow and functions, let's take a look at how curly brackets and parentheses are used in Swift.

Curly brackets

In Swift, unlike other C-like languages, curly brackets are required for conditional statements and loops. In other C-like languages, if there is only one statement to execute for a conditional statement or a loop, the curly brackets around that line are optional. This has lead to numerous errors and bugs, like the Apple's goto fail bug; therefore, when Apple was designing Swift, they decided to keep curly brackets, even when there is only one line of code to execute. Let's look at some code that illustrates this. This first example is not valid in Swift because it is missing the curly brackets; however, it will be valid in most other languages:

if (x > y)
  x=0

In Swift, you are required to have the curly brackets, as illustrated in the following example:

if (x > y) {
  x=0
}

Parentheses

Unlike other C-like languages, the parentheses around conditional expressions in Swift are optional. In the preceding example, we put parentheses around the conditional expression but they are not required. The following example would be valid in Swift but not valid in most C-like languages:

if x > y {
  x=0
}
主站蜘蛛池模板: 牡丹江市| 灵台县| 滦平县| 黄大仙区| 阿拉善左旗| 体育| 东光县| 鄯善县| 红河县| 达州市| 巩留县| 云梦县| 舒兰市| 绥江县| 泾源县| 鲁甸县| 寿光市| 团风县| 葫芦岛市| 仙游县| 汽车| 如东县| 浪卡子县| 齐齐哈尔市| 肇东市| 托克托县| 江都市| 梅州市| 万全县| 沐川县| 大港区| 资阳市| 凤城市| 九龙县| 葫芦岛市| 陆河县| 苗栗市| 天等县| 阿合奇县| 德清县| 南靖县|