3

Possible Duplicate:
What is the best way to solve an Objective-C namespace collision?

I have develop a third-party dynamic library for iphone, depends on Mobile Substrate. The problem is that if some APP have a Obj-C class named "Class1", while my dylib also have a class named "Class1", there is no guarantee that which class will be used at run time. Is there any solution that I can keep the class name "Class1" and solve the namespace problem?

Community
  • 1
  • 1
Kevin Cao
  • 1,332
  • 1
  • 8
  • 13
  • I've heard about Two-level namespaces on Mac OS.But on iphone platform seems to be flat namespace? – Kevin Cao Oct 14 '12 at 06:36
  • There is no easy solution. See [What is the best way to solve an Objective-C namespace collision?](http://stackoverflow.com/questions/178434/what-is-the-best-way-to-solve-an-objective-c-namespace-collision) The namespace is flat for your purposes, but being pedantic it's not (see Bavarious comment on that link) – Jano Oct 14 '12 at 06:41
  • 1
    Apple always seems to prefix their own code with two letters. CL for core location, CA for core animation, NS for the base data types. – Nathan Villaescusa Oct 14 '12 at 06:41
  • Check this answer: http://stackoverflow.com/questions/178434/what-is-the-best-way-to-solve-an-objective-c-namespace-collision – detunized Oct 14 '12 at 08:41

1 Answers1

2

In Objective-C the concept of namespace doesn't exist, source files are simply imported one in another and this of course can cause name collisions if common name are used as class name.

A good way to solve collision problems in Objective-C is adopt a prefix to differentiate your class name from other class names, and this is the technique already used by Apple itself, lets think about the array class, it is called NSArray, why ?

There is a specific reason behind that :

  • N : identify Next
  • S : identify Step
  • Array : identify the name of the data structure

NextStep is the name of the company that created the Foundation framework, and the company that was acquired by Apple in the mid 90's.

I'm for example using a different prefix for my classes that I pack in libraries, I use : AleArray first 3 characters of my name followed by the name of the data structure, you can choose your own prefix and go ahead with that ...

Of course, the probability of a collision using a simple prefix still higher than using namespaces(that usually use a reverse domain name notation), but could be enough in most situations.

aleroot
  • 71,077
  • 30
  • 176
  • 213