> For the complete documentation index, see [llms.txt](https://philm.gitbook.io/philm-ios-wiki/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://philm.gitbook.io/philm-ios-wiki/mei-zhou-yue-du/swift-ming-ming-kong-jian.md).

# swift命名空间

1、在oc中我们通过类名能非常方便的动态到创建对象。

```
- (id)productWithClassName:(NSString *)claName{

    class cla = NSClassFromString(claName);
    if (cla) {

        id obj = [[cla alloc] init];
        return obj;
    }
}
```

但是如果将这段代码直接转换成swift代码，一运行是会直接闪退的，因为swift中是存在命名空间的。swift中 `import UIKit` 等都是导入对应到命名空间，swift中同一个命名空间类是不需要 import 。swift中类的完成命名应该是"命名空间"+`.`+"类名"。

下面是一个通过配置 tableView的dataSource 来跳转不同到界面

```
class ViewController: UIViewController {

    public var dataSource:[[String:String]] = [["title":"pageOne","class":"PageOneViewController"],
                                               ["title":"pageTwo","class":"PageTwoViewController"]];
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.title = "首页"
        let tableView:UITableView = UITableView.init(frame: self.view.bounds, style: UITableViewStyle.plain);
        tableView.delegate = self;
        tableView.dataSource = self;
        self.view.addSubview(tableView);

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
```

```
extension ViewController:UITableViewDelegate,UITableViewDataSource{

    func numberOfSections(in tableView: UITableView) -> Int {

        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return dataSource.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        var cell :UITableViewCell? = tableView.dequeueReusableCell(withIdentifier: "cellId")
        if cell == nil {

            cell = UITableViewCell.init(style: .default, reuseIdentifier: "cellId")
        }
        let dic:Dictionary = dataSource[indexPath.row]
        cell?.textLabel?.text = dic["title"]
        return cell as UITableViewCell!;
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
    {
        tableView.deselectRow(at: indexPath, animated: true)

        let vcName:String = dataSource[indexPath.row]["class"]!
        let title:String  = dataSource[indexPath.row]["title"]!
        // 0.获取命名空间
        guard let executable = Bundle.main.infoDictionary!["CFBundleExecutable"] as? String else {
            return
        }

        // 1.获取对应的类
        guard let childVcClass : AnyClass = NSClassFromString(executable + "." + vcName) else {
            return
        }

        let childClass = childVcClass as! UIViewController.Type
        let childVc = childClass.init()
        childVc.title  = title;
        self.navigationController?.pushViewController(childVc, animated: true);

    }
}
```

关键代码： 先动态取到当前项目的命名空间， 然后通过命名空间和类名拼成完整到类名

```
  // 0.获取命名空间
        guard let executable = Bundle.main.infoDictionary!["CFBundleExecutable"] as? String else {
            return
        }
            // 1.获取对应的类
        guard let childVcClass : AnyClass = NSClassFromString(executable + "." + vcName) else {
            return
        }
```
