Swift 5.2 A Swift Tour

快速浏览

传统建议使用新语言的第一个程序应打印“ Hello,world!”字样。 屏幕上。 在Swift中,这可以单行完成:

print("Hello, World!")
// Print "Hello, World!"

如果您使用C或Objective-C编写代码,则此语法看起来很熟悉-在Swift中,这行代码是完整的程序。 您无需导入单独的库即可使用输入/输出或字符串处理等功能。 在全局范围内编写的代码用作该程序的入口点,因此您不需要main()函数。 您也不需要在每个语句的末尾写分号。

本教程通过向您展示如何完成各种编程任务,为您提供了足够的信息来开始在Swift中编写代码。 如果您不了解某些内容,请不要担心,本教程的其余部分将详细介绍此导览中介绍的所有内容。

注意
为了获得最佳体验,请在Xcode 的Playgrounds 中打开本章。 在Playgrounds 上,您可以编辑代码清单并立即查看结果。

简单的价值观

使用let来创建常量,使用var来生成变量。 常量的值不需要在编译时就知道,但是您必须为它赋值一次。 这意味着您可以使用常量来命名一次确定但在许多地方使用的值。

var myVariable = 42
myVariable = 50
let myConstant = 42

常量或变量的类型必须与要分配给它的值的类型相同。 但是,您不必总是显式地编写类型。 在创建常量或变量时提供一个值可让编译器推断其类型。 在上面的示例中,编译器推断myVariable是整数,因为其初始值是整数。

如果初始值不能提供足够的信息(或者没有初始值),请通过在变量后加一个冒号来指定类型,以指定类型。

let implicitInteger = 70
let implicitDouble = 70.0
let explicitDouble: Double = 70

print(explicitDouble)
// Print "70.0"

实验

创建一个显式类型为Float且值为4的常量。

值永远不会隐式转换为另一种类型。 如果需要将值转换为其他类型,请显式创建所需类型的实例。

let label = "The width is "
let width = 94
let widthLabel = label + String(width)
print(widthLabel)

// Print "The width is 94"

实验

尝试从最后一行删除到String的转换。 你得到什么错误?

Binary operator '+' cannot be applied to operands of type 'String' and 'Int'

二进制运算符'+'不能应用于'String'和'Int'类型的操作数

还有一种更简单的方法可以在字符串中包含值:在括号中写值,并在括号前写反斜杠(\)。 例如:

let apples = 3
let oranges = 5
let appleSummary = "I have \(apples) apples."
let fruitSummary = "I have \(apples + oranges) pieces of fruit."

print(appleSummary)
// Print "I have 3 apples."
print(fruitSummary)
// Print "I have 8 pieces of fruit."

实验

使用\()在字符串中包含浮点计算,并在问候语中包含某人的名字。

let age = 12
let name = "Jack"
let Introduction = "My name is \(name). My age is \(age)"
print(Introduction)

// Print "My name is Jack. My age is 12"

对于占用多行的字符串,请使用三个双引号(“”“)。只要每个引号行的开头与右引号的缩进匹配,都将删除该缩进。例如:

let apples = 3
let oranges = 5

let quotation = """
I said "I have \(apples) apples."
And then I said "I have \(apples + oranges) pieces of fruit."
"""

print(quotation)

//Print "I said "I have 3 apples.""
//Print "And then I said "I have 8 pieces of fruit.""

使用方括号([])创建数组和字典,并通过在方括号中写入索引或键来访问它们的元素。 最后一个元素后允许使用逗号。

var shoppingList = ["catfish", "water", "tulips"]
shoppingList[1] = "bottle of water"

var occupations = [
    "Malcolm": "Captain",
    "Kaylee": "Mechanic",
]
occupations["Jayne"] = "Public Relations"

print(shoppingList)
// ["catfish", "bottle of water", "tulips"]
print(occupations)
// ["Jayne": "Public Relations", "Malcolm": "Captain", "Kaylee": "Mechanic"]

数组随着添加元素而自动增长。

var shoppingList = ["catfish", "water", "tulips"]
shoppingList[1] = "bottle of water"

shoppingList.append("blue paint")
print(shoppingList)

//["catfish", "bottle of water", "tulips", "blue paint"]

若要创建一个空数组或字典,请使用初始化语法。

let emptyArray = [String]()
let emptyDictionary = [String: Float]()

如果可以推断类型信息,则可以将空数组写为[],将空字典写成[:],例如,当您为变量设置新值或将参数传递给函数时。

var shoppingList = ["catfish", "water", "tulips"]
var occupations = [
    "Malcolm": "Captain",
    "Kaylee": "Mechanic",
]

shoppingList = []
occupations = [:]

print(shoppingList)
// []

print(occupations)
// [:]
发布了4 篇原创文章 · 获赞 12 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Luckyep/article/details/105035161