-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathScanner.swift
More file actions
57 lines (50 loc) · 1.66 KB
/
Scanner.swift
File metadata and controls
57 lines (50 loc) · 1.66 KB
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
//
// Scanner.swift
// CSModuleManager
//
// Created by CoderStar on 2022/11/25.
//
import Foundation
/// 扫描策略协议
public protocol ClassScannableStrategy {
var types: [AnyClass] { get set }
/// 是否符合指定要求
func isMatched(anyClass: AnyClass) -> Bool
/// 对符合指定要求的类进行处理
func handle(anyClass: AnyClass)
}
public class Scanner {
/// 扫描
/// - Parameter strategies: 扫描策略
public static func scan(strategies: [ClassScannableStrategy]) {
if canEnumerateClasses() {
/// 快速查找
enumerateClasses(false) { type in
for strategy in strategies {
if strategy.isMatched(anyClass: type) {
strategy.handle(anyClass: type)
}
}
}
} else {
/// 慢速查找
let typeCount = Int(objc_getClassList(nil, 0))
if typeCount > 0 {
let types = UnsafeMutablePointer<AnyClass>.allocate(capacity: typeCount)
defer {
types.deallocate()
}
let autoreleasingTypes = AutoreleasingUnsafeMutablePointer<AnyClass>(types)
objc_getClassList(autoreleasingTypes, Int32(typeCount))
for index in 0 ..< typeCount {
let type: AnyClass = types[index]
for strategy in strategies {
if strategy.isMatched(anyClass: type) {
strategy.handle(anyClass: type)
}
}
}
}
}
}
}