Objective-C equivalent of Java packages?

后端 未结 6 1288
青春惊慌失措
青春惊慌失措 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: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.

提交回复
热议问题