IOS study Note 3-namespace

Source: Internet
Author: User

First, I would like to add that this series is not a tutorial. In addition to errors, this series will not be written in a simple order. What theme to write depends on your mood.

After C # is switched to Objective-c development, the following question occurs:

Does Objective-C seem to have no namespace?

Yes. C ++ and C # Have namespace and Java have Package (of course there is a difference with Namespace). php I recently wrote is also like namespace com \ example \ johnslibrary \ people; even JavaScript... Well let's forget JavaScript, it should have no built-in namespace (see http://elegantcode.com/2011/01/26/basic-javascript-part-8-namespaces/ and http://addyosmani.com/blog/essential-js-namespacing ).

When developing C #, you will often encounter classes with the same name under different namespaces. In this case, you can use alias to distinguish between them.
For example

using n1=Namespace1;using n2=Namespace2;n1.ClassA=new n1.ClassA();n2.ClassA=new n2.ClassA();

 

But Objective-C does not.

Similar to c, everything runs in the same global namespace.
Therefore, when creating an Objective-c application, there will be a text box for you to enter the prefix ).
"NS" is the prefix of built-in Cocoa, indicating NextStep. This prefix is occupied by Apple. The relationship between NextStep and Mac OS is not much said.

However, compared with general NameSpace names such as CompanyA. XXX. YYY, prefixes are prone to conflicts. If I start a company called MoeSoft, the prefix may conflict with Microsoft.
If the MoeSoft and Microsoft class libraries with the MS prefix are referenced at the same time, it may be a tragedy.

Stackoverflow provides the following solutions:
Use NSBundle to load one of the frameworks, copy and rename the framework, and then load another namespace.

Another suggestion is to use @ compatibility_alias to "RENAME" the class.

For details, see:
Http://stackoverflow.com/questions/178434/what-is-the-best-way-to-solve-an-objective-c-namespace-collision

If this problem can be avoided, try to avoid it. What are the idioms? The gentleman does not stand under the Dangerous wall.

So unless you are writing the simplest test code, you 'd better give your application a unique prefix long enough.

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.