通常您想在.m文件中使用空白类别来声明私有属性。
// APXCustomButton.m file
@interface APXCustomButton()
@property (nonatomic, strong) UIColor *stateBackgroundColor;
@end
// Use the property in implementation (the same .m file)
@implementation APXCustomButton
- (void)setStyle:(APXButtonStyle)aStyle
{
UIColor *theStyleColor = ...;
self.stateBackgroundColor = theStyleColor;
}
@end
如果您尝试访问.m文件以外的黑色类声明的属性,您将收到未申报财产编译器错误:
- (void)createButton
{
APXCustomButton *theCustomButton = [[APXCustomButton alloc] init];
theCustomButton.stateBackgroundColor = [UIColor greenColor]; // undeclared property error
}
在大多数情况下,如果你想添加新的方法/属性没有子现有的类,然后要在h文件和实施的.m文件
// APXSafeArray.h file
@interface NSArray (APXSafeArray)
- (id)com_APX_objectAtIndex:(NSInteger)anIndex;
@end
// APXSafeArray.m file
@implementation NSArray
- (id)com_APX_objectAtIndex:(NSInteger)anIndex
{
id theResultObject = nil;
if ((anIndex >= 0) && (anIndex < [self count]))
{
theResultObject = [self objectAtIndex:anIndex];
}
return theResultObject;
}
@end
现在你可以使用“com_APX_objectAtInde声明的方法申报类别x:“方法,只要导入了”APXSafeArray.h“。
#import "APXSafeArray.h"
...
@property (nonatomic, strong) APXSafeArray *entities;
- (void)didRequestEntityAtIndex:(NSInteger)anIndex
{
APXEntity *theREquestedEntity = [self.entities com_APX_objectAtIndex:anIndex];
...
}
http://cupsofcocoa.com/2011/03/27/objective-c-lesson-8-categories/ – nsgulliver 2013-02-28 11:24:22
http://stackoverflow.com/questions/3967187/difference-between-interface-definition -in-h-and-m-file – trojanfoe 2013-02-28 11:25:50
应该研究objective-c中的私有和公共变量 – 2013-02-28 11:27:39