In C#, namespaces essentially serve 2 specific purposes:
- Avoid naming collisions
- Inform a class of objects that are available.
Lets take a look at the latter first:
Using vs Import
C#
Objective-C
As you can see, Objective-C does not have a concept of namespaces. Everything is essentially file based. You basically are informing each class about what ever interfaces that have been defined in other header files.
The import statement with quotes means the the header file has been defined in this project. The other import statements have been defined in another framework, where as the first part is the framework name, and the second part is the header file being referenced. A general rule that most frameworks follow, is to create a header file with the same name as the framework, that has import stmts to all publicly exposed classes in the framework.
While imports seem generally straight forward, they can easily lead to confusion and frustration if you don't fully understand how to use them. Probably the most common issue is when you define 2 objects that have references to each other. This generally happens when you have a protocol and a property on your class that consumes it:
If you were to try and compile the code above, you'd immediately run into a compiler error. The problem is, that since each class has a dependency on the other, the compiler can't figure out what to do.
That kind of sucks :) But relax, there is a very simple solution, you'll need to use the "@class" compiler directive.
Basically, all we're are doing is telling the compiler that the "MyCustomObject" will exist at some point, so just pretend its there for now.
Ok, so thats probably the simplest way at looking at the problem, and was easy to solve b/c the protocol and interface were defined in the same file. But what happens when you define 2 objects in separate files.
Above we have to separate header files, with 2 different objects, that each have a reference to each other and if you try and compile you'll get an error. So how do we sove this?
Well believe it or not, its pretty much the same solution:
However, we're not completely done, if we try to access the obj property in the MyOtherCustomObject implementation, we'll get another compiler exception.
So what did we miss? Well, b/c we use @class in the header, we didn't' actually inform the class about the MyCustomObject, we basically told it to just pretend that its there for now. So when we tried to actually access this pretend class the compiler got angry at us.
But, again the solution turns out to be simple. In the Implementation file we can import the MyCustomObject header file, b/c the compiler has already done its walkthrough and validated all of our objects.
And just like that, we've solved the problem. Obviously this is no where as intuitive as namespaces in C#, but once you begin to understand how the compiler works and to take advantage of importing header files, it does become second nature.
Naming Collisions
In the beginning of this post i mentioned two important problems that namespaces solve. And with Import statements you solve one of the issues. However, it does not solve the collision issue.
In reality the best way to avoid collisions is to simply name your classes carefully. Its a really good idea to include prefixes on all of your class names so that they remain unique.
However, if you're stuck using another framework that didn't follow those guidelines, you can try using the following compiler directive:
@compatibility_alias NewClassName OriginalClassName;
With this directive, you can rename a competing class, and thus fix the naming conflict you've run into. I've never actually run into this issue before, but its a good tool to have in your toolbox in case you ever do.
Well that about wraps up my explanation of namespaces. As always I hope this was helpful.
If you did find this interesting and hunger for more comparisons of C# and Objective-C, you can read more of this series on my blog.
If there is a topic you've been waiting for me to talk about that I haven't covered yet, let me know on twitter:@codeBySteveZ
-SteveZ