This article is divided into two parts:
First, methods in Swift are nested
Ii. Namespaces in Swift
first, methods in Swift are nested
In Swift we can let methods nest methods, such as:
func Appendquery (Var url:string, key:string, Value:anyobject)-String {func appendquerydictionary (url:string, key:string, Value: [String:anyobject])-String {//... return "result"} func Appendqueryarray (url:string, key:string, value : [Anyobject])-String {//... return "result"} func Appendquerysingle (url:string, key:string, Value:anyobject)-String {//... return "result" } ifLet dictionary = value as?[String:anyobject] {returnappendquerydictionary (URL, Key:key, value:dictionary)}Else ifLet array = value as?[Anyobject] {returnappendqueryarray (URL, Key:key, Value:array)}Else { returnAppendquerysingle (URL, key:key, value:"value") }}
ii. namespaces in Swift
We used to use OC development, it is no namespace, all the code and reference to the static library will eventually be compiled into the same domain and binary, the consequence is that once we have duplicate class name, it will lead to compile-time conflicts and failures. To avoid this, the type of OC typically adds two to three letters of prefix, such as Mjextense,mjrefresh,mbprogresshud. However, the prefix does not imply a guarantee of non-conflict, and Swift uses the namespace to solve the problem for us
//Myframework.swift//This file exists in the Myframework.framework Public classMyClass { Public classfunc Hello () {print ("Hello from Framework") }}//Myapp.swift//This file exists in the app's main targetclassMyClass {classfunc Hello () {print ("Hello from App")}}myclass.hello ()//Hello from AppMyFramework.MyClass.hello ()//Hello from Framework
Another strategy is to use nested types of methods to specify the scope of access
structMyClassContainer1 {classMyClass {classfunc Hello () {print ("Hello from MyClassContainer1") } }}structMyClassContainer2 {classMyClass {classfunc Hello () {print ("Hello from MyClassContainer2") } }}//when used:MyClassContainer1.MyClass.hello () MyClassContainer2.MyClass.hello ( )
So we can avoid using some weird prefixes.
Swift develops eighth-method nesting & namespaces