swift 命名空间

Swift 与 Objective-C 不同,它拥有命名空间的概念,这使得在 Swift 中直接使用类名可能会导致运行时错误。在 Swift 中,`import` 语句用于导入命名空间,而完全限定类名应包含命名空间和类名。例如,为了动态创建对象并配置 tableView 的 dataSource,我们需要正确地引用命名空间和类。关键操作包括获取项目命名空间,并据此拼接完整类名。

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
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值