以上步骤对应的代码如下:
- (void)registerPath:(NSString *)path actionBlock:(RouterActionBlock)actionBlock {
YTRouterActionObject *actionObject = [YTRouterActionObject new];
actionObject.path = path;
actionObject.actionBlock = actionBlock;
NSMutableDictionary *subRouter = [self subRouterMapWithPath:path];
subRouter[YTRouterActionObjectKey] = actionObject;
}
- (NSMutableDictionary *)subRouterMapWithPath:(NSString *)path {
NSArray *components = [path componentsSeparatedByString:@"/"];
NSMutableDictionary *subRouter = self.routerMap;
for (NSString *component in components) {
if (component.length == 0) {
continue;
}
if (!subRouter[component]) {
subRouter[component] = [NSMutableDictionary new];
}
subRouter = subRouter[component];
}
return subRouter;
}
在Demo中注册的几个路由最终的配置如下,比如home/messagelist对应的路由配置保存在<YTRouterActionObject: 0x6040000365e0>对象中
Printing description of self->_routerMap:
{
home = {
"_" = "<YTRouterActionObject: 0x60c00003b040>";
messagelist = {
"_" = "<YTRouterActionObject: 0x6040000365e0>";
detail = {
"_" = "<YTRouterActionObject: 0x600000038ec0>";
};
getmessage = {
"_" = "<YTRouterActionObject: 0x600000038e80>";
};
};
};
}
路由使用实现
路由使用实现时序图:










