在一些Apple Iphone示例中,一些属性在头文件中声明,而一些属性在实现文件中声明。例如,在Siesmic XML示例中
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
实现文件中附加接口声明的用途是什么?
答案 0 :(得分:5)
这只是公共和私有类接口之间的区别。标题描述了公共接口,但是某些属性仅供类本身使用,而不是由其协作者使用。这些私有属性通常以您描述的方式声明,作为实现文件中的类别或类扩展。
// 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