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.
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() { super.viewDidLoad() print(self) } // The print result is:<AA.ViewController: 0x7fec6a00e5c0>
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
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 print(namespace!)
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.
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]; ... }
Add a controller to //viewDidLoad override func viewDidLoad() { super.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 }