Objective-C equivalent of Java packages?

Go To StackoverFlow.com

15

What is the Objective-C equivalent of Java packages? How do you group and organize your classes in Objective-C?

2009-06-16 10:35
by Arne Evertsson
Retagged 'Java' as 'Cocoa' so Java people don't get confused/angry about what is really an Objective-C question. :- - Quinn Taylor 2009-06-16 17:08
Ok, but does the question have more to do with Cocoa - Arne Evertsson 2009-06-16 19:27
This question has nothing to do with coco - Jason Coco 2009-06-18 07:42
You have a point. Although Objective-C is strongly tied with Cocoa, this question deals with the language and not the Cocoa framework specifically. so I'm removing the Cocoa tag - Quinn Taylor 2009-06-18 17:52


31

Question 1: Objective-C equivalent of Java packages?

Objective-C doesn't have an equivalent to Java packages or C++ namespaces. Part of the reason for this is that Objective-C was originally a very thin runtime layer on top of C, and added objects to C with minimum fuss. Unfortunately for us now, naming conflicts are something we have to deal with when using Objective-C. You win some, you lose some...

One small clarification (although it's not much for consolation) is that Objective-C actually has two flat namespaces — one for classes and one for protocols (like Java's interfaces). This doesn't solve any class naming conflicts, but it does mean you can have a protocol and class with the same name (like <NSObject> and NSObject) where the latter usually adopts ("implements") the former. This feature can prevent "Foo / FooImpl" pattern rampant in Java, but sadly doesn't help with class conflicts.

Question 2: How to [name] and organize Objective-C classes?

Naming

The following rules are subjective, but they are decent guidelines for naming Objective-C classes.

  1. If your code can't be run by other code (it's not a framework, plugin, etc. but an end-user application or tool) you only need to avoid conflicts with code you link against. Often, this means you can get away with no prefix at all, so long as the frameworks/plugins/bundles you use have proper namespaces.
  2. If you're developing "componentized" code (like a framework, plugin, etc.) you should choose a prefix (hopefully one that's unique) and document your use of it someplace visible so others know to avoid potential conflicts. For example, the CocoaDev wiki "registry" is a de facto public forum for calling "dibs" on a prefix. However, if your code is something like a company-internal framework, you may be able to use a prefix that someone else already does, so long as you aren't using anything with that prefix.

Organization

Organizing source files on disk is something that many Cocoa developers unfortunately gloss over. When you create a new file in Xcode, the default location is the project directory, right beside your project file, etc. Personally, I put application source in source/, test code (OCUnit, etc.) in test/, all the resources (NIB/XIB files, Info.plist, images, etc.) in resources/, and so on. If you're developing a complex project, grouping source code in a hierarchy of directories based on functionality can be a good solution, too. In any case, a well-organized project directory makes it easier to find what you need.

Xcode really doesn't care where your files are located. The organization in the project sidebar is completely independent of disk location — it is a logical (not physical) grouping. You can organize however you like in the sidebar without affecting disk location, which is nice when your source is stored in version control. On the other hand, if you move the files around on disk, patching up Xcode references is manual and tedious, but can be done. It's easiest to create your organization from the get-go, and create files in the directory where they belong.

My Opinion

Although it could be nice to have a package/namespace mechanism, don't hold your breath for it to happen. Class conflicts are quite rare in practice, and are generally glaringly obvious when they happen. Namespaces are really a solution for a non-problem in Objective-C. (In addition, adding namespaces would obviate the need for workarounds like prefixes, but could introduce a lot more complexity in method invocation, etc.)

The more subtle and devious bugs come from method conflicts when methods are added and/or overridden, not only by subclasses, but also be categories, which can cause nasty errors, since the load order of categories is undefined (nondeterministic). Implementing categories is one of the sharpest edges of Objective-C, and should only be attempted if you know what you're doing, particularly for third-party code, and especially for Cocoa framework classes.

2009-06-16 15:50
by Quinn Taylor
So you're saying logical organization in Xcode, which is independent of disk location and more for developer convenience, is the only way to organize classes - raffian 2012-06-18 23:05


4

They use long names...

2009-06-16 10:37
by instanceof me
Unfortunately, it looks like dotnetdevelopersjournal.com is gone - ThomasW 2012-03-22 07:01
Thanks for the heads up, I added a link to an archive. It's ugly but the text is there - instanceof me 2012-04-02 09:48


3

See What is the best way to solve an Objective-C namespace collision? for a discussion of how Objective-C has no namespaces, and the painful hacks this necessitates.

2009-06-16 10:39
by Matthew Flaschen


1

Unfortuantely objective c doesn't have any equivalent to namespace of C#,c++ and package of java....

The naming collisions could be solved by giving contextual name for example if u gonna give a name to method it should imply the class and module that it comes in so that...these problems could be avoided.

Go through the following url to know more on naming convention as advised by apple

http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/ProgrammingWithObjectiveC/Conventions/Conventions.html

2013-07-10 12:22
by Durai Amuthan.H


0

What about something like this (inside a directory)?

 #define PruebaPaquete ar_com_oxenstudio_paq1_PruebaPaquete
@interface ar_com_oxenstudio_paq1_PruebaPaquete : NSObject {

and importing it like this:

 #import "ar/com/oxenstudio/paq1/PruebaPaquete.h"
 PruebaPaquete *p = [[PruebaPaquete alloc] init];

and when you have name collision:

 #import "ar/com/oxenstudio/paq1/PruebaPaquete.h"
 #import "ar/com/oxenstudio/paq2/PruebaPaquete.h"


ar_com_oxenstudio_paq1_PruebaPaquete *p = [[ar_com_oxenstudio_paq1_PruebaPaquete alloc] init];
ar_com_oxenstudio_paq2_PruebaPaquete *p2 = [[ar_com_oxenstudio_paq2_PruebaPaquete alloc] init];
2010-05-16 01:46
by Lauaro


-1

Well, I think all the other answers here seem to focus on naming collisions, but missed at least one important feature, package private access control that java package provides.

When I design a class, I find it is quite often that I just want some specific class(es) to call its methods, b/c they work together to achieve a task, but I don't want all the other unrelated classes to call those methods. That is where java package access control comes in handy, so I can group the related classes into a packaged and make those methods package private access control. But there is no way to do that in objective c.

Without package private access control I find it is very hard to avoid people writing code like this, [[[[[a m1] m2] m3] m4] m5] or [a.b.c.d m1].

Update: Xcode 4.4 introduced "An Objective-C class extension header", in my opinion, that is in some way to provide "package private access control", so if you include the extension header, you can call my "package private" methods; if you only include my public header, you can only call my public API.

2013-08-22 07:55
by Qiulang
Ads