Skip to content

Commit cbe771d

Browse files
committed
refine: 增加兼容OC协议声明以及实现代替继承
1 parent dcff2a5 commit cbe771d

File tree

10 files changed

+87
-68
lines changed

10 files changed

+87
-68
lines changed

Example/TheRouter.xcodeproj/project.pbxproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
1C839BA02AA6F67A00726596 /* TheRouterControllerA.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1C839B922AA6F67A00726596 /* TheRouterControllerA.swift */; };
1717
1C839BA12AA6F67A00726596 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 1C839B932AA6F67A00726596 /* Main.storyboard */; };
1818
1C839BA22AA6F67A00726596 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1C839B942AA6F67A00726596 /* AppDelegate.swift */; };
19-
1C839BA32AA6F67A00726596 /* TheRouterControllerB.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1C839B962AA6F67A00726596 /* TheRouterControllerB.swift */; };
2019
1C839BA42AA6F67A00726596 /* TheRouterBController.m in Sources */ = {isa = PBXBuildFile; fileRef = 1C839B972AA6F67A00726596 /* TheRouterBController.m */; };
2120
1C839BA52AA6F67A00726596 /* TheRouterControllerD.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1C839B982AA6F67A00726596 /* TheRouterControllerD.swift */; };
2221
1C839BA62AA6F67A00726596 /* TheRouterController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1C839B992AA6F67A00726596 /* TheRouterController.swift */; };
@@ -54,7 +53,6 @@
5453
1C839B932AA6F67A00726596 /* Main.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = Main.storyboard; sourceTree = "<group>"; };
5554
1C839B942AA6F67A00726596 /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
5655
1C839B952AA6F67A00726596 /* TheRouterBController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TheRouterBController.h; sourceTree = "<group>"; };
57-
1C839B962AA6F67A00726596 /* TheRouterControllerB.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TheRouterControllerB.swift; sourceTree = "<group>"; };
5856
1C839B972AA6F67A00726596 /* TheRouterBController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TheRouterBController.m; sourceTree = "<group>"; };
5957
1C839B982AA6F67A00726596 /* TheRouterControllerD.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TheRouterControllerD.swift; sourceTree = "<group>"; };
6058
1C839B992AA6F67A00726596 /* TheRouterController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TheRouterController.swift; sourceTree = "<group>"; };
@@ -138,7 +136,6 @@
138136
1C839B972AA6F67A00726596 /* TheRouterBController.m */,
139137
1C839B992AA6F67A00726596 /* TheRouterController.swift */,
140138
1C839B922AA6F67A00726596 /* TheRouterControllerA.swift */,
141-
1C839B962AA6F67A00726596 /* TheRouterControllerB.swift */,
142139
F0750B132B5EA112003FD724 /* LARouterController.swift */,
143140
F0750B152B5EA72A003FD724 /* AnyPrifxClassTestController.swift */,
144141
1C839B8E2AA6F67A00726596 /* TheRouterControllerC.swift */,
@@ -384,7 +381,6 @@
384381
F0750B142B5EA112003FD724 /* LARouterController.swift in Sources */,
385382
1C839BA22AA6F67A00726596 /* AppDelegate.swift in Sources */,
386383
1C41F0152B0DD793002E2F24 /* TheRouterControllerE.swift in Sources */,
387-
1C839BA32AA6F67A00726596 /* TheRouterControllerB.swift in Sources */,
388384
1C839BA42AA6F67A00726596 /* TheRouterBController.m in Sources */,
389385
1C839B9D2AA6F67A00726596 /* TheRouterApi.swift in Sources */,
390386
1C839B9E2AA6F67A00726596 /* TheRouterWebController.swift in Sources */,

Example/TheRouter/AppDelegate.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
3131
// - excludeCocoapods: 是否对Cocoapods生成的组件进行动态注册
3232
// - excludeCocoapods = true 不对Cocoapods生成的组件进行动态注册, false 对Cocoapods生成的组件也进行遍历动态注册
3333
// - useCache: 是否开启本地缓存功能
34-
TheRouterManager.loadRouterClass(excludeCocoapods: true, useCache: true)
34+
TheRouterManager.loadRouterClass(excludeCocoapods: true, useCache: false)
3535

3636
TheRouter.lazyRegisterRouterHandle { url, userInfo in
3737
TheRouterManager.injectRouterServiceConfig(webRouterUrl, serivceHost)

Example/TheRouter/TheRouterApi.swift

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,7 @@ public class TheRouterAApi: NSObject, CustomRouterInfo {
3636
public class TheRouterBApi: NSObject, CustomRouterInfo {
3737

3838
public static var patternString = "scheme://router/demo2"
39-
public static var routerClass = "TheRouter_Example.TheRouterControllerB"
40-
public var params: [String: Any] { return [:] }
41-
public var jumpType: LAJumpType = .push
42-
43-
public override init() {}
44-
}
45-
46-
public class TheRouterBAndroidApi: NSObject, CustomRouterInfo {
47-
48-
public static var patternString = "scheme://router/demo2-Android"
49-
public static var routerClass = "TheRouter_Example.TheRouterControllerB"
39+
public static var routerClass = "TheRouterBController"
5040
public var params: [String: Any] { return [:] }
5141
public var jumpType: LAJumpType = .push
5242

Example/TheRouter/TheRouterBController.m

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
//
88

99
#import "TheRouterBController.h"
10-
11-
@interface TheRouterBController ()
10+
@import TheRouter;
11+
@interface TheRouterBController () <TheRouterableProxy>
1212

1313
@end
1414

@@ -32,4 +32,19 @@ - (UILabel *)desLabel {
3232
return _desLabel;
3333
}
3434

35+
// 实现协议中的类方法
36+
+ (NSArray<NSString *> *)patternString {
37+
return @[@"scheme://router/demo2"];
38+
}
39+
40+
+ (NSUInteger)priority {
41+
return TheRouterPriorityDefault;
42+
}
43+
44+
+ (id)registerActionWithInfo:(NSDictionary<NSString *, id> *)info {
45+
TheRouterBController *vc = [[TheRouterBController alloc] init];
46+
vc.desLabel.text = info.description;
47+
return vc;
48+
}
49+
3550
@end

Example/TheRouter/TheRouterControllerB.swift

Lines changed: 0 additions & 28 deletions
This file was deleted.

Example/TheRouter/ViewController.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,9 @@ class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSour
135135
let model = TheRouterModel.init(name: "AKyS", age: 18)
136136
TheRouter.openURL(("scheme://router/demo?id=2&value=3&name=AKyS&desc=通过TheRouterManager.addGloableRouter()传入registerClassPrifxArray参数,将指定遍历工程中具有特性前缀名的class,降低遍历数量级,减少性能损耗", ["model": model, "clouse": clouse]))
137137
case 5:
138-
139-
let model = TheRouterModel.init(name: "AKyS", age: 18)
140-
TheRouter.openURL(("scheme://router/demo2?id=2&value=3&name=AKyS&desc=这是一个OC类的界面,实现路由的跳转需要继承OC类,并实现TheRouterAble协议即可", ["model": model]))
138+
// let model = TheRouterModel.init(name: "AKyS", age: 18)
139+
// TheRouter.openURL(("scheme://router/demo2?id=2&value=3&name=AKyS&desc=这是一个OC类的界面,实现路由的跳转需要继承OC类,并实现TheRouterAble协议即可", ["model": model]))
140+
TheRouter.openURL(TheRouterBApi().requiredURL)
141141
case 6:
142142

143143
let model = TheRouterModel.init(name: "AKyS", age: 18)

README.md

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ TheRouter.removeRouter(TheRouterViewCApi.patternString)
351351
@property (nonatomic, strong) UILabel *desLabel;
352352
@end
353353

354-
@interface TheRouterBController ()
354+
@interface TheRouterBController ()<TheRouterableProxy>
355355

356356
@end
357357

@@ -363,25 +363,22 @@ TheRouter.removeRouter(TheRouterViewCApi.patternString)
363363
[self.view addSubview:self.desLabel];
364364
// Do any additional setup after loading the view.
365365
}
366-
@end
367366

368-
public class TheRouterControllerB: TheRouterBController, TheRouterable {
367+
// 实现协议中的类方法
368+
+ (NSArray<NSString *> *)patternString {
369+
return @[@"scheme://router/demo2"];
370+
}
369371

370-
public static var patternString: [String] {
371-
["scheme://router/demo2",
372-
"scheme://router/demo2-Android"]
373-
}
374-
375-
public static var descriptions: String {
376-
"TheRouterControllerDemo"
377-
}
372+
+ (NSUInteger)priority {
373+
return 1;
374+
}
378375

379-
public static func registerAction(info: [String : Any]) -> Any {
380-
let vc = TheRouterBController()
381-
vc.desLabel.text = info.description
382-
return vc
383-
}
376+
+ (id)registerActionWithInfo:(NSDictionary<NSString *, id> *)info {
377+
TheRouterBController *vc = [[TheRouterBController alloc] init];
378+
vc.desLabel.text = info.description;
379+
return vc;
384380
}
381+
@end
385382
```
386383

387384
### 路由根据版本号缓存能力

TheRouter/Classes/TheRouter+Convenience.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,17 @@ public extension TheRouter {
3030
/// - patternString: register urlstring
3131
/// - classString: the class which match the className need inherit the protocol of TheRouterable
3232
class func addRouterItem(_ patternString: String, priority: uint = 0, classString: String) {
33+
3334
let clz: AnyClass? = classString.trimmingCharacters(in: CharacterSet.whitespaces).la_matchClass()
3435
if let routerable = clz as? TheRouterable.Type {
3536
self.addRouterItem(patternString.trimmingCharacters(in: CharacterSet.whitespaces), priority: priority, handle: routerable.registerAction)
3637
} else {
37-
shareInstance.logcat?(patternString, .logError, "\(classString) register router error, please implementation the TheRouterable Protocol")
38-
assert(clz as? TheRouterable.Type != nil, "register router error, please implementation the TheRouterable Protocol")
38+
if let currentCls = clz, currentCls.self.conforms(to: TheRouterableProxy.self) {
39+
self.addRouterItem(patternString.trimmingCharacters(in: CharacterSet.whitespaces), priority: priority, handle: currentCls.registerAction)
40+
} else {
41+
shareInstance.logcat?(patternString, .logError, "\(classString) register router error, please implementation the TheRouterable Protocol")
42+
assert(clz as? TheRouterable.Type != nil, "register router error, please implementation the TheRouterable Protocol")
43+
}
3944
}
4045
}
4146

TheRouter/Classes/TheRouterManager.swift

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,14 +190,14 @@ extension TheRouterManager {
190190

191191
for i in 0 ..< resultXLClass.count {
192192
let currentClass: AnyClass = resultXLClass[i]
193+
193194
if let cls = currentClass as? TheRouterable.Type {
194195
let fullName: String = NSStringFromClass(currentClass.self)
195196
if fullName.contains(kSADelegateClassSensorsSuffix) {
196197
break
197198
}
198199

199200
for s in 0 ..< cls.patternString.count {
200-
201201
if fullName.contains(NSKVONotifyingPrefix) {
202202
let range = fullName.index(fullName.startIndex, offsetBy: NSKVONotifyingPrefix.count)..<fullName.endIndex
203203
let subString = fullName[range]
@@ -206,6 +206,21 @@ extension TheRouterManager {
206206
registerRouterList.append([TheRouterPath: cls.patternString[s], TheRouterClassName: fullName, TheRouterPriority: "\(cls.priority)"])
207207
}
208208
}
209+
} else if currentClass.self.conforms(to: TheRouterableProxy.self) {
210+
let fullName: String = NSStringFromClass(currentClass.self)
211+
if fullName.contains(kSADelegateClassSensorsSuffix) {
212+
break
213+
}
214+
215+
for s in 0 ..< currentClass.patternString().count {
216+
if fullName.contains(NSKVONotifyingPrefix) {
217+
let range = fullName.index(fullName.startIndex, offsetBy: NSKVONotifyingPrefix.count)..<fullName.endIndex
218+
let subString = fullName[range]
219+
registerRouterList.append([TheRouterPath: currentClass.patternString()[s], TheRouterClassName: "\(subString)", TheRouterPriority: "\(String(describing: currentClass.priority()))"])
220+
} else {
221+
registerRouterList.append([TheRouterPath: currentClass.patternString()[s], TheRouterClassName: fullName, TheRouterPriority: "\(String(describing: currentClass.priority()))"])
222+
}
223+
}
209224
}
210225
}
211226
let endRegisterTime = CFAbsoluteTimeGetCurrent()
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
// TheRouterableProxy.h
3+
// TheRouter
4+
//
5+
// Created by 姚亚杰 on 2024/4/8.
6+
//
7+
8+
#import <Foundation/Foundation.h>
9+
10+
typedef NS_OPTIONS(NSUInteger, TheRouterPriority) {
11+
TheRouterPriorityLow = 1000,
12+
TheRouterPriorityDefault = 1001,
13+
TheRouterPriorityHeight = 1002
14+
};
15+
16+
NS_ASSUME_NONNULL_BEGIN
17+
18+
@protocol TheRouterableProxy <NSObject>
19+
20+
// 使用类方法替代静态属性
21+
+ (NSArray<NSString *> *)patternString;
22+
23+
+ (NSUInteger)priority;
24+
25+
// 静态方法可以直接转换为类方法
26+
+ (id)registerActionWithInfo:(NSDictionary<NSString *, id> *)info;
27+
@end
28+
29+
NS_ASSUME_NONNULL_END

0 commit comments

Comments
 (0)