Objective-C equivalent of Java packages?

后端 未结 6 1196
青春惊慌失措
青春惊慌失措 2021-02-07 11:35

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

相关标签:
6条回答
  • 2021-02-07 12:12

    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.

    0 讨论(0)
  • 2021-02-07 12:18

    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];
    
    0 讨论(0)
  • 2021-02-07 12:21

    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.

    0 讨论(0)
  • 2021-02-07 12:22

    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

    0 讨论(0)
  • 2021-02-07 12:28

    They use long names...

    • Article on coding style & naming in Cocoa / Objective-C
    • Discussion whether Obj-C needs namespaces (deleted, archive here)
    0 讨论(0)
  • 2021-02-07 12:29

    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.

    0 讨论(0)
提交回复
热议问题