Swift's namespace

Source: Internet
Author: User

Scene:

The class name in the project is too long to distinguish it from the file name, and if a module has more classes, it is difficult to get a more elegant name. To make the module name readable, it is common practice to add a module prefix. However, if the module also has sub-modules, if you continue to follow this method, the file name will be particularly long.

1. First level (Me Module)

Memaincontroller

Meeditcontroller Meaboutcontroller

2. Level Two (Me > Profile Module)

Meprofilechangepasswordcontroller

Meprofilechangeavatorcontroller

This time the name of the file is too long to be elegant, think of our intention to take such a long name, just want to reach the project in the class name unique, readable non-conflicting

NameSpace

Swift's namespace, learned C + +, PHP know, and they are very different. The Swift namespace is based on the module

Instead of explicitly specifying in the code, each module represents a namespace in Swift. In other words, the type name in the same target

Still can't be the same. When we do app development, the content that is added by default to the app's main target is in the same namespace.

1. Different target, here is not the burden here, please refer to my reference > namespace below for a detailed explanation

2. How do I achieve this namespace effect in a project?

1. Include with struct
//
struct Packageone {
}extension Packageone { class class { var name:string init (name:string) { self.name = name } } }print ("\ (PackageOne.Class.self)") Let TMP = Packageone.class (name: "Hell") print (TMP)//class with class TestClass {} Extension TestClass { class MyClass { var name:string init (name:string) { self.name = name } }} Let test = Testclass.myclass (name: "Test") print (test)

From the code above, enlighten us on the specific application in the project:

1. For view

Project use: struct Memodule {    struct views{}    struct viewcontrollers{}}//This is a viewextension memodule.views {    Class edit:uiview{                        }}extension memodule.views {    class Update:uiview {            }}

2. For Viewcontroller

This is a controllerextension memodule.viewcontrollers {    class Edit:uiviewcontroller {                }}extension memodule.viewcontrollers {    class Update:uiviewcontroller {        }    }

Use Naspace to make the class name unique, the above two cases, for the organization of the folder we will

Memodule>views>edit> Edit.swift

Memodule> Views>update>update.swift

Memodule>viewcontrollers>edit>editcontroller.swift

Memodule>viewcontrollers>update>updatecontroller.swift

Makes the file unique.

Reference:
1. Namespaces

2. How do I use the Namespace in Swift?

Swift's namespace

Related Article

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.