关联对象Associated Object,相关函数
1
2
3objc_getAssociatedObject(id _Nonnull object, const void * _Nonnull key)
objc_setAssociatedObject(id _Nonnull object, const void * _Nonnull key,
id _Nullable value, objc_AssociationPolicy policy)可能的应用场景:给系统控件增加属性
- 关键字:dynamic 动态属性
runtime
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15appInit() {
let test = class_getInstanceMethod(UIViewController.self, #selector(UIViewController.viewDidLoad))
let test2 = class_getInstanceMethod(UIViewController.self, #selector(UIViewController.newMethod))
method_exchangeImplementations(test!, test2!)
}
extension UIViewController {
@objc func newMethod() {
#if DEBUG
DLog("\(type(of: self)) was loaded by \(type(of: self.parent))")
#endif
self.newMethod()
}
}struct和class之间的抉择:
a. 默认选struct: 拷贝赋值型,影响相对较小
b. 与OC交互时,需要使用class型
c. 需要区分不同实例的时候,使用class(操作符===)KVO/KVC/Keypath/NSKeyValueObservation !!!
单例
1
2
3
4
5
6
7
8
9
10
11
12
13//通用单例
class Singleton {
static let sharedInstance = Singleton()
}
//如果初始化的时候,需要进行其他操作
class Singleton {
static let sharedInstance: Singleton = {
let instance = Singleton()
// setup code
return instance
}()
}自动打包 fastlane
iOS开发能力提升
Title:iOS开发能力提升
Author:Lynn Cheng
Created:2019-08-02, 14:15:48
Updated:2019-08-20, 09:28:27
Full URL:http://lynncheng.github.io/2019/08/02/iOS提升/
License: "CC BY-NC-SA 4.0" Keep Link & Author if Distribute.