看代码 学习Swift

Swift

本篇文章主要是在看 其他人代码的过程中 看到的一些知识点 这里做一下总结

调用对象的方法

这里是对Selector写了个分类,让代码看起来更简单

1
2
3
4
5
extension Selector {
static let endEditing = #selector(UIView.endEditing(_:))
}
// dismiss keyboard
self.view.addGestureRecognizer(UITapGestureRecognizer(target: self.view, action: Selector.endEditing))

方法的参数判断

对于常见的一些方法为Optional的参数 判断方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
func getLabelsInfo() -> (name: String?, work: String?, salary: String?) {
guard let name = nameTextField.text,
let work = workTextField.text,
let salary = salaryLabel.text
else {
return (nil, nil, nil)
}

if name.isEmpty || work.isEmpty || salary.isEmpty {
return (nil, nil, nil)
}

return (name, work, salary)
}

对象的判空

.some 表示对象有值 .none表示对象为空

1
2
3
4
5
6
7
8
9
10
11
12
switch tweet {
case .some(let tweet):
showAlert(title: "Love Tweet",
message: tweet.info,
buttonTitle: "OK")

case .none:
showAlert(title: "Info miss or invalid",
message: "Please fill out correct personal info",
buttonTitle: "OK")
}

属性声明

private 私有 类内部私有
fileprivate 文件内部私有 一个文件有多个类
public 外部可使用 类可以继承 方法可以重载
open 外部可使用 类不可以继承 方法不可以重载

1
2
fileprivate let mainStopwatch: Stopwatch = Stopwatch()

代理声明

1
2
weak var delegate: PokemonSelectionDelegate?

赋值

1
2
(cell.titleLabel.text, cell.descriptionLabel.text, cell.dateLabel.text) = (item.title, item.description, item.pubDate)

注册cell

1
view.register(FBMeBaseCell.self, forCellReuseIdentifier: FBMeBaseCell.identifier)

UI配置文件

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
public struct Specs {
public struct Color {
public let tint = UIColor(hex: 0x3b5998)
public let red = UIColor.red
public let white = UIColor.white
public let black = UIColor.black
public let gray = UIColor.lightGray
}

public struct FontSize {
public let tiny: CGFloat = 10
public let small: CGFloat = 12
public let regular: CGFloat = 14
public let large: CGFloat = 16
}

public struct Font {
private static let regularName = "Helvetica Neue"
private static let boldName = "Helvetica Neue Bold"
public let tiny = UIFont(name: regularName, size: Specs.fontSize.tiny)
public let small = UIFont(name: regularName, size: Specs.fontSize.small)
public let regular = UIFont(name: regularName, size: Specs.fontSize.regular)
public let large = UIFont(name: regularName, size: Specs.fontSize.large)
public let smallBold = UIFont(name: boldName, size: Specs.fontSize.small)
public let regularBold = UIFont(name: boldName, size: Specs.fontSize.regular)
public let largeBold = UIFont(name: boldName, size: Specs.fontSize.large)
}

public struct ImageName {
public let friends = "fb_friends"
public let events = "fb_events"
public let groups = "fb_groups"
public let education = "fb_education"
public let townHall = "fb_town_hall"
public let instantGames = "fb_games"
public let settings = "fb_settings"
public let privacyShortcuts = "fb_privacy_shortcuts"
public let helpSupport = "fb_help_and_support"
public let placeholder = "fb_placeholder"
}

public static var color: Color {
return Color()
}

public static var fontSize: FontSize {
return FontSize()
}

public static var font: Font {
return Font()
}

public static var imageName: ImageName {
return ImageName()
}
}

16进制颜色

1
2
3
convenience init(hex: Int) {
self.init(r: (hex & 0xff0000) >> 16, g: (hex & 0xff00) >> 8, b: (hex & 0xff), a: 1)
}

多个判断条件叠加

1
2
3
4
guard let index = viewController.photoIndex, index != photos.count - 1 else {
return nil
}

常量的位置

文件中共用的常量要放到类的外面,否则可能无法在某些类方法或者对象方法中使用

控制器代码结构

控制器结构