# 01.纯Swift路由方案探究

前言

> `OC` 写了几年了到了新公司开始写`Swift` 项目正在成长期, 开始人也不多, 模块化程度不高 于是就整了这套路由方案 发现现在很多Swift路由方案很大程度上还是使用了`OC`的特性比如需要继承`NSObject` 作为一名`Swift`萌新, 这次路由方案的设计是很好的一次对`Swift`的探索

## 回顾历史 Objective-c

以往路由方案的核心主要是 `+Load注册` `反射` 等

### + Load

```swift
+ (void)Load {
    // 注册唯一标示 
}
```

### 反射

生成类

```swift
    Class myClass = NSClassFromString(@"DetailViewController")
```

方法调用

```swift
    SEL action = NSSelectorFromString(selectorName);

    if (![self respondsToSelector:action]) {
        return;
    }

    IMP imp = [self methodForSelector:action];
    void (*func)(id, SEL, id) = (void *)imp;
    func(self, action, data);
```

### 调度形式

```swift
 // Target-Action
 [self performTarget:@"ModuleA" action:@"Func" params:@{ @"name":@"OldWang" }]

 // URL
 [self open:@"demo://ModuleA/showDetail?id=100"]

 // ...
```

### 问题

* `Swift`中没有 `+Load`
* `Swift`包含命名空间, 反射较为麻烦, 且必须继承`NSObject`才能反射
* 硬编码多维护成本较高, 无法编译检查&#x20;

## RYRouter

![RouterUML02-2](http://ohfpqyfi7.bkt.clouddn.com/RouterUML02-2.jpg)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ryukiedev.gitbook.io/wiki/swift/chun-swift-lu-you-fang-an-tan-jiu.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
