前言:网上一直没有找到用Swift开发IOS的好的教程,所以找了官网的文档翻译一下算了。如有错误欢迎指正。博主首发CSDN,mcf171专栏。
博客链接:mcf171的博客
原文链接:Learn the Essentials of Swift
——————————————————————————————
第一节课是通过Swift playground进行展现,playground允许我们修改code并且直接在Xcode上马上看到结果。Playground对于学习和实验也是一个不错的助手,能够让我们加速对于Swift 概念的基础学习。
可以下载以下课程文件在Xcode中打开从而进行学习。
学习目标
在本次课程中,你可以了解到:
- 常量和变量的不同
- 明白什么时候使用隐式声明什么时候用显示声明
- 理解使optionals的好处和optional绑定
- optionals和隐式 unwrapped optionals的区别
- 理解条件语句的目的和循环
- 使用switch语句来进行条件分支
- 使用where关键字来增加在条件语句中的额外约束
- 函数,方法和初始化的不同
- 类,结构体和枚举的不同
- 理解继承和接口
- 使用Xcode的quick help快捷键来决定隐式类型和发现额外信息。
- Import和使用UIKit
基本类型
常量就是指在第一次声明之后它的值不再进行改变。相应的变量就是之后值可以进行变换的。如果我们知道一个值将不会在我们的代码中改变,那么可以将它声明为常量而不是变量。
使用let来声明常量,var声明变量
var myVariable = 42
myVariable = 50
let myConstant = 42
ps:在Xcode中,option+click可以查看一个常量和变量的类型。可以在Xcode上进行试验。
变量不会自动的从一个类型隐式转换为其他的类型。如果需要讲一个值转换为不同的类型,那么要进行显示的描述。
let label = "The width is "
let width = 94
let widthLabel = label + String(width)
ps:可以尝试移除最后一行的String,看看能出现什么错误。
同时也有一个简单的方法在string中加入值:在括号中写值。同时在括号前面加一个反斜杠(\)。
let apples = 3
let oranges = 5
let appleSummary = "I have \(apples) apples."
let fruitSummary = "I have \(apples + oranges) pieces of fruit."
使用optionals来处理可能为空的值,一个optional值可以包含一个确切的值或者nil(空值)。通过在类型后面加?号来标记值为optional
let optionalInt: Int? = 9
(博主暂时也不太了解这个)为了理解一个optional的潜在类型,那么需要拆包(unwrap)这个变量。在后面将进一步学习unwrapping optionals,最直接unwrap的方法是在变量后面加一个!,只有确定这个值不是nil的时候才可以使用这个操作。
let actualInt: Int = optionalInt!
ps:大家可以尝试去掉:Int会报错。
var myString = "7"
var possibleInt = Int(myString)
print(possibleInt)
myString = "banana"
possibleInt = Int(myString)
print(possibleInt)
ar ratingList = ["Poor", "Fine", "Good", "Excellent"]
ratingList[1] = "OK"
ratingList
可以使用以下语法创建一个空的数组
// Creates an empty array.
let emptyArray = [String]()
var implicitlyUnwrappedOptionalInt: Int!
在之后我们自己的代码中可能很少用到上述的隐式unwrapped optional。但是在之后的课程和API中你会经常看到这种表示。
控制流
Swift有两种控制流语句。条件语句:比如说if和switch。循环语句:for-in和while。
(具体描述省略)
let number = 23
if number < 10 {
print("The number is small")
} else if number > 100 {
print("The number is pretty big")
} else {
print("The number is between 10 and 100")
}
ps:修改number的值来观测不同的number值对于最后输出的影响
这些语句可以进行嵌套来进行创建复杂、有趣的一些行为。下面是一个for-in中嵌入了一个if-else语句
let individualScores = [75, 43, 103, 87, 12]
var teamScore = 0
for score in individualScores {
if score > 50 {
teamScore += 3
} else {
teamScore += 1
}
}
print(teamScore)
在if语句中使用optional赋值(optional binding)来检测optional是否包含一个值
var optionalName: String? = "John Appleseed"
var greeting = "Hello!"
if let name = optionalName {
greeting = "Hello, \(name)"
}
ps:将optionalName设置为nil,看看会有什么变化。
同时也可以使用一个if语句来赋予多个值。where关键字用来增加额外的约束。下述代码当且仅当所有的赋值成功。
var optionalHello: String? = "Hello"
if let hello = optionalHello where hello.hasPrefix("H"), let name = optionalName {
greeting = "\(hello), \(name)"
}
switch 在Swift中是非常强有力的。一个switch语句支持任何类型的数据和多种多样的比较操作符。在下述例子中,switch语句是对于vegetable字符串的值进行分值。
let vegetable = "red pepper"
switch vegetable {
case "celery":
let vegetableComment = "Add some raisins and make ants on a log."
case "cucumber", "watercress":
let vegetableComment = "That would make a good tea sandwich."
case let x where x.hasSuffix("pepper"):
let vegetableComment = "Is it a spicy \(x)?"
default:
let vegetableComment = "Everything tastes good in soup."
}
ps:可以尝试一掉default语句看会出现什么错误
var firstForLoop = 0
for i in 0..<4 {
firstForLoop += i
}
print(firstForLoop)
这个半开的范围操作符 ..< 不包含上边界。所以范围是[0,4) 如果使用的是... 则包含两个端点。
-
var secondForLoop = 0 for _ in 0...4 { secondForLoop += 1 } print(secondForLoop)
上述代码是从0到4,一共迭代5次。下划线是代表一个通配符,当我们没必要知道当前执行的迭代是什么的时候,我们可以使用下划线。