0

我从一个 iOS 项目中得到了一个简单的 Object-C 代码片段:

@interface store (private)

@end

@implementation store (private)

@end

@implementation store

我的问题是:

  1. 代码、私有接口和实现中的含义是什么(private)

  2. 最后一行@implementation store是什么意思?一个空的公共实现?没有@end

  3. 由于上面的代码中有两个@implementation store,这是否意味着objective-c支持单个接口的多个实现?

4

3 回答 3

1
  1. What does (private) mean in the code, private interface & implementation?-(private)表示您正在贴花/实现一个 Objective-C类别。在这种情况下private只是一个名称。如果是的话就没什么区别了store (myPrivateMethods)

  2. What does the last line @implementation store mean? An empty public implementation? Without @end?-@implementation store是类的实际实现部分store。不确定如果@end缺少 an 会发生什么。

  3. Since there are two @implementation store in above code, does that mean objective-c support multiple implementation for a single interface?- 实际上没有 2 个实现store。该类有一个实现和该类store的一个类别的一个实现 - store (private)

于 2013-04-18T14:29:28.847 回答
0
  1. 这是一个objective-c 类别,允许您将代码添加到现有类。括号中的名称是任意的。
  2. 这是一个空的实现,但我很惊讶它编译时没有@end. 使用默认编译器设置尝试 xcode,我得到一个错误。如果只是为了清楚起见,它应该有一个@end.
  3. 第一个实现是第一个的附加。您可以使用它将大类分解为单独的模块,使接口的某些部分对某些人可用而不对其他人可用,或者 - 最有用和最常见的 - 向 SDK 类添加方法。
于 2013-04-18T14:18:12.320 回答
0

private means nothing in Objective-C, you call it peanuts if you want. It's really a label to differentiate categories if you have multiple, but it's value is arbitrary. It just has to mean something to you as a developer.

The @implementation store line marks the beginning of the implementation for the class defined by @interface store (i.e. no category), which is probably declared in a '.h' file. It's the fact that the interface is in the '.h' that makes it public (because other classes can import the .h file and therefore see the declaration of properties, methods, etc...).

It doesn't support multiple implementations, they are additive. Of course, then you have the question of implementing the same method in both. This is strongly discouraged by Apple. Here's the doc.

Avoid Category Method Name Clashes

Because the methods declared in a category are added to an existing class, you need to be very careful about method names.

If the name of a method declared in a category is the same as a method in the original class, or a method in another category on the same class (or even a superclass), the behavior is undefined as to which method implementation is used at runtime. This is less likely to be an issue if you’re using categories with your own classes, but can cause problems when using categories to add methods to standard Cocoa or Cocoa Touch classes.

于 2013-04-18T14:10:08.817 回答