开发者

What’s the point of repeated class interface declaration in the implementation file?

In some of the Apple Iphone examples, some of the properties are declared in the header file and some properties in the implementation file. For example in the Siesmic 开发者_开发百科XML examples

ParseOperation.h

@interface ParseOperation : NSOperation {
NSData *earthquakeData;

@private
NSDateFormatter *dateFormatter;

// these variables are used during parsing
Earthquake *currentEarthquakeObject;
Contact *currentContactObject;
NSMutableArray *currentParseBatch;
NSMutableString *currentParsedCharacterData;

BOOL accumulatingParsedCharacterData;
BOOL didAbortParsing;
NSUInteger parsedEarthquakesCounter;
}

@property (copy, readonly) NSData *earthquakeData;

@end

ParseOperation.m

@interface ParseOperation () <NSXMLParserDelegate>
  @property (nonatomic, retain) Earthquake *currentEarthquakeObject;
  @property (nonatomic, retain) NSMutableArray *currentParseBatch;
  @property (nonatomic, retain) NSMutableString *currentParsedCharacterData;
  @property (nonatomic, retain) Contact *currentContactObject;
@end

What is the use of the additional interface declaration in the implementation file ?


That’s simply a difference between a public and a private class interface. The header describes the public interface, but some of the properties are only meant to be used by the class itself, not by its collaborators. These private properties are usually declared the way you described, as a category or a class extension inside the implementation file.

// Foo.h – the public interface
@interface Foo : NSObject {…}

// Collaborators can only read bar.
@property(readonly) int bar;
@property(readonly) int baz;

@end

// Foo.m
#import "Foo.h"

// Private interface
@interface Foo ()

// Inside class implementation we can also change bar.
@property(assign) int bar;
@property(assign) int other;

@end

@implementation Foo
@synthesize bar, baz, other;
…
@end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜