Namespaces in Swift

namespacenamespace A common concept within C++ and C#, Swift also introduces such a mechanism, so here's an exploration of the ins and outs of this namespace.

I. Why you need namespaces

In a nutshell: to avoid naming conflicts

In development, especially in multi-module development, it is difficult to ensure that class names are not duplicated between modules. In order to ensure that classes with the same name under different modules can be used normally without reporting errors, namespaces are introduced to ensure that even if the class names created are the same, as long as the namespaces are different, these classes are not the same, so this is a safety mechanism to prevent conflicts with namespaces. It can be seen that the full form of a class name in Swift is actually "namespace + class name". We can try to see the full name by printing the current class in the class.

override func viewDidLoad() {
// The print result is:<AA.ViewController: 0x7fec6a00e5c0>

2、 namespace View & Modify

From the above printout, it looks like the namespace is the name of our project, so what if we view it? We need to open it in source formInfo.plist , you can see that there is a field insideCFBundleExecutable, It corresponds to the value of namespace。

View namespace.png

If you want to modify the namespace, Be careful not to edit directlyInfo.plist , you can enterBuild Settings Search inProduct Name , then make the changes.

Modify namespace.png

III. How namespaces are obtained

Now that we know that we can passInfo.plist Get the namespace, so how do you get it in your program? It obviously needs to be parsedInfo.plist File, getCFBundleExecutable The corresponding value of the value.

let namespace = Bundle.main.infoDictionary!["CFBundleExecutable"]
 // An optional type is returned      

IV. The use of namespaces in development

There is a common scenario in development where a customTabBarController The problem is often that a controller (class) is created by a controller name (string). Here's a comparison between the Objective-C and Swift implementations of the two languages.

  • Since there are no namespaces in Objective-C, it's easy to write.
Add a controller to //viewDidLoad
- (void)viewDidLoad {
    [super viewDidLoad];
    [self addNavigationChildVC:@"ContactViewController" :@" contact person" :@"tabbar_contacts" :@"tabbar_contactsHL"];

 // Create a controller in a custom method based on the string passed in
-(void)addNavigationChildVC: (NSString *) vcName :(NSString *)title :(NSString *)nomalImageName :(NSString *)selectedImageName {
     // Create a controller
    Class class = NSClassFromString(vcName);
    UIViewController *vc = [[class alloc]init];
  • Namespaces exist in Swift, and if you don't get the desired result by following the above approach, it's time to figure out how to handle it
Add a controller to //viewDidLoad
override func viewDidLoad() {
    addChildViewController(vcName: "ContactsViewController", title: " contact person", image: "tabbar_contacts", selectedImage: "tabbar_contactsHL")

 // Create a function to translate the name of the controller into a concrete class
func stringToVC(vcName:String) -> UIViewController? {
     //Get the namespace
    guard let namespace = Bundle.main.infoDictionary!["CFBundleExecutable"] as? String else {
        print(" Failed to get")
        return nil
     // Splice the complete class
    guard let vcClass = NSClassFromString(namespace + "." + vcName) else {
        print(" Splice failure")
        return nil
     // Convert to UIViewController
    guard let vcType = vcClass as? UIViewController.Type else {
        print(" Conversion failure")
        return nil
     // Create the corresponding controller based on the type
    let vc = vcType.init()
    return vc

1、Blockchain engineers make over a million dollars a year but need to have super skills
2、Meitu Cloud joins forces with CAS to propose behavior classification technology based on interactionaware attention mechanism neural network
3、Multiperson online collaboration tools
4、NetEase Releases BlockchainBased EcoValue Sharing Platform the Planet
5、257kmh Azera EP9 sets world record for driverless speed

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送