美文网首页swift
swift中的命名空间

swift中的命名空间

作者: 梁森的简书 | 来源:发表于2021-04-28 10:20 被阅读0次

    OC中没有命名空间,如果直接使用两个不同库,而且这两个库中都使用了同一个流行的三方,那么就会产生冲突。
    swift中,不同命名空间(module)的类即使同名也不会冲突。
    我们在项目中创建的类完全可以和pod库中的类同名。

    由于命名空间的存在,我们无法通过NSClassFromString获取到类,如果要想获取到类,需要在字符串前加上“.”和“命名空间” 。
    代码:

    if let cls = NSClassFromString(getAPPName() + "." + "FirstViewController") as? UIViewController.Type {
                let vc = cls.init()
                self.present(vc, animated: true)
            } else {
                debugPrint("nil...")
            }
    
    func getAPPName() -> String {
            guard let appName = Bundle.main.object(forInfoDictionaryKey: "CFBundleExecutable") as? String else { return "" }
            return appName
        }
    

    相关文章

      网友评论

        本文标题:swift中的命名空间

        本文链接:https://www.haomeiwen.com/subject/edusvktx.html