swift start

Swift

1
2
3
4
5
6
7
8
9
10
11
class Game {
var bugs: Int
var hasMusic: Bool
var levels: Int
init(bugs: Int, hasMusic: Bool, levels: Int) {
self.bugs = bugs
self.hasMusic = hasMusic
self.levels = levels
}
}

func

1
2
3
4
5
6
有返回值的func
func nameOfFunction(/* parameters */) -> Type {
var valueToReturn: Type
// 函数的其余代码
return valueToReturn
}

一开始设置了默认的值:0.15

2.func进阶:

==函数重载==

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct House {
let numberOfBedrooms: Int
let numberOfBathrooms: Int
}
func doubleMe(number: Int) -> Int {
return number * 2
}
func doubleMe(string: String) -> String {
return string + string
}
func doubleMe(house: House) -> House {
return House(numberOfBedrooms: house.numberOfBedrooms * 2,
numberOfBathrooms: house.numberOfBathrooms * 2
}

在 Swift 中这是可行的,因为编译器的智能足以检测函数调用时传入的参数类型,并选择正确的 doubleMe 函数

在此,每个函数调用对应 doubleMe 的版本,对于后者,自变量类型匹配参数类型。这种定义函数的方式称为函数重载,因为我们已经根据参数“重载”了单个函数名称,从而具有许多含义。

==元组==

1
2
3
4
let myTuple = ("Question 1", false, false, true)
print(myTuple.0) // prints "Question 1"
print(myTuple.3) // prints "true"
1
2
3
4
5
6
func funcName(paraments:type) -> (type1,type2) {
return value1,value2 //value1,value2 的类型分别为 type1,type2
}
let bytuple = funcName(paraments:argument)
bytuple.0 //得到返回的value1
bytuple.1 //得到返回的value2
1
2
3
4
5
6
如果是这样-> (up: Bool, right: Bool, down: Bool, left: Bool, numberOfWalls: Int)
那么就可以myTuple.up
myTuple.down
myTuple.left
myTuple.numberOfWalls
这样通过点运算来取值

==In-Out参数==

1
2
3
4
5
6
如果我们希望对参数的更改在调用函数后保留下来,则需要将参数声明为输入-输出参数(注意:您无法将常量作为输入-输出参数传递到函数中):如上图
为声明参数为 in-out,我们在参数名称前面添加了关键字 inout。此外,我们还必须在通过引用传递值的参数名称前面放一个与号 (&)。“通过引用传递”意味着不创建副本,在函数内部修改参数,而函数外部的参数都是相同的。您在上面可以看到,stats 在函数内进行了修改,在函数调用之后,这些更改仍然保留。
注意:In-out 参数与函数的返回值是不一样的。
in-out 参数让我们能够在函数内部更改参数值,并将这些更改反映在函数外部,我们必须注意这些类型的参数。

==内部与外部参数名==

就是调用函数的时候省略掉了parament

1
2
3
4
5
6
func addExcitementToString(_ string: String) -> String {
return string + "!"
}
addExcitementToString("Swift") //这二句是一样的
addExcitementToString(string: "Swift") // 我们需要忽略掉它!
1
2
3
4
5
func combineStrings(_ s1: String, _ s2: String) -> String {
return s1 + s2
}
combineStrings("We love", " Swift!")

==enum==

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
enum Season {
case Fall
case Winter
case Spring
case Summer
}
let myFavoriteSeason = Season.Fall
var favoriteActivity = ""
switch (myFavoriteSeason) {
case .Fall:
favoriteActivity = "seeing the leaves change color."
case .Winter:
favoriteActivity = "skiing."
case .Spring:
favoriteActivity = "seeing the wildflowers bloom"
case .Summer:
favoriteActivity = "swimming in the river"
}

字符串

请记住,属性和方法与特定类型有关联:属性好比值,而方法好比函数(它们本质相同,只是属性和方法是设计层面的术语,而值和函数是程序层面的术语。)。

==reverse() 翻转==

1
2
3
4
// Reverse the characters in a string 翻转:比如,把zby 翻转为ybz
var forwardString = "spoons"
var charactersReversed = forwardString.characters.reversed() //reversed()方法返回所有字符的集合而不是一个字符串
var backwardsString = String(charactersReversed) //使用 String 初始化方法来它转化为字符串类型。

字符串组

1
2
3
4
5
6
7
8
9
10
11
12
// Concatenation
let theTruth = "Money can't buy me love"
let alsoTrue = "but it can buy me a boat."
let combinedTruths = theTruth + ", " + alsoTrue
// Finding a substring within a string
var word = "fortunate"
word.contains("tuna")
// Replacing a substring
var password = "Mary had a little loris"
var newPassword = password.replacingOccurrences(of: "a", with: "A")

零值

可选值

option

可选值类型可作为参数传递给函数

1
2
3
4
5
6
7
func pickUpGroceries(car:Car?) {
if let car = car {
print("We'll pick up the groceries in the (car.model)")
} else {
print("Let's walk to the store")
}
}

此函数容纳可选值类型 Car? 的参数。如果变量 car 有值,则开车到杂货店。如果 car 为零值,则步行到商店。以下是这两种情况可能显示的内容:

var someCar = Car(make: “Toyota”, model: “Corolla”)
pickUpGroceries(someCar)
然后输出可能为: We’ll pick up the groceries in the Corolla

在为零值的情况下:

pickUpGroceries(nil)
输出为: Let’s walk to the store

数组

1
2
3
4
5
6
7
8
// The verbose way
var numbers = Array<Double>()
// More often you will see ...
var moreNumbers = [Double]()
// Array literal syntax
let differentNumbers = [97.5, 98.5, 99.0]

热评文章