swift 数据类型 class protocols Enumerations

1
2
3
4
5
6
7
struct Rectangle {
//属性
var width : Int
var height : Int
}
var rectangle = Rectangle()
rectangle.height = 4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
struct Rectangle {
//属性
var width : Int
var height : Int
var other : 12
//初始化方法
init(width: Int, height: Int) {
self.width = width
self.height = height
}
//函数
func fitsInside(_ other: Rectangle) -> Bool {
return (width < other.width) && (height < other.height)
}
//自动计算值
var area: Int {
return width * height
}
}

Extensions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
struct Rectangle {
var width : Int
var height : Int
init(width: Int, height: Int) {
self.width = width
self.height = height
}
}
extension Rectangle {
func fitsInside(_ other: Rectangle) -> Bool {
return (width < other.width) && (height < other.height)
}
var area: Int {
return width * height
}
}

泛型结构

1
2
3
4
5
6
7
8
9
10
11
12
struct NamedArray<Element> {
var name: String
var items: [Element]
}
//写法1
let boardGame :NamedArray<String> = NamedArray(name: "Board Games", items:["Chess", "Go"])
let primes: NamedArray<Int> = NamedArray(name: "Primes", items: [1,3,5,7,13])
//写法2 (推荐)
let boardGame = NamedArray(name: "Board Games", items:["Chess", "Go"])
let primes = NamedArray(name: "Primes", items: [1,3,5,7,13])

class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Fish {
//属性
var name: String
//初始化
init(name: String) {
self.name = name
}
//方法
func swim() {
print("I'm swimming.")
}
}
//继承
class FlyingFish: Fish {
func fly() {
print("fly")
}
}
class ComplainingFish: Fish {
var complaint: String
//重写初始化方法
init(name: String, complaint: String) {
self.complaint = complaint
super.init(name: name)
}
重写父类方法
override func swim() {
print("123")
super.swim()
}
}

protocols

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
protocol Player {
func takeTurn(on board: Board)
}
struct HumanPlayer: Player {
func takeTurn(on board: Board) {
}
}
struct ComputerPlayer: Player {
var name: String
var score: Int
func takeTurn(on board: Board) {
}
}
let player1 = ComputerPlayer.init(name: "fuck", score: 1)
print(player1)
//使用 extension 关键字会打印相关信息
extension ComputerPlayer: CustomStringConvertible {
var description: String {
return "human player (name) has a score of (score)"
}
}

Enumerations

1
2
3
4
enum Alignment {
case left
case right
}

等同于

1
2
3
enum Alignment {
case left, right
}

1
let textAlignment = Alignment.left
1
2
3
4
5
6
switch textAlignment {
case Alignment.left:
print("1")
case Alignment.right:
print("2")
}

等同于

1
2
3
4
5
6
switch textAlignment {
case .left:
print("12")
case .right:
print("1")
}

其他用法

1
2
3
4
5
6
7
8
9
10
11
12
13
enum Alignment {
case left(padding: Double), right(padding: Double), center
}
let textAlignment = Alignment.left(padding: 42.7)
switch textAlignment {
case .left(let padding):
print("Left with (padding) pixels of padding")
case .right(let padding):
print("Left with (padding) pixels of padding")
case .center:
print("center")
}
1
2
3
4
5
6
7
8
9
10
11
enum ServerAddress: String {
case google = "https://www.google.com"
case apple = "https://www.apple.com"
}
func findAddress(matchingQuery: String, from server: ServerAddress) {
let serviceAddress = server.rawValue
print(serviceAddress)
}
findAddress(matchingQuery: "11", from: .google)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
enum SomeError: ErrorProtocol {
case somethingWentWrong, somethingFailed
}
func doSomething() throws -> Data {
progressBar.visible = true
defer { progressBar.visible = false }
let data: Data?
do {
data = try somethingThatMightFail()
} catch SomeError.somethingWentWrong {
data = nil
}
guard let result = summarize(data) else { throw SomeError.somethingFailed }
return result
}