Swift develops eighth-method nesting & namespaces

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.