我需要在UIViewController中添加一个实例变量和两个使用该实例变量的方法。我在类扩展中为该变量添加了一个属性。然后我为UIViewController创建了一个包含方法名称的分类。该分类头文件导入了类扩展。
要使用该分类,我将其导入到我的自定义视图控制器中。但是,当我调用其中一个使用扩展中声明的属性的分类方法时,它会崩溃。
我可以通过在UIViewController子类中合成属性来绕过此问题,但我认为这些应该自动合成。
我遗漏了什么吗?
UIViewController_CustomObject.h(扩展头文件)
#import <UIKit/UIKit.h>
#import "CustomObject.h"
@interface UIViewController ()
@property (nonatomic, strong) CustomObject *customObject;
@end
UIViewController+CustomObject.h(类别头文件)
#import <UIKit/UIKit.h>
#import "UIViewController_CustomObject.h"
@interface UIViewController (CustomObject)
- (void)customMethod;
UIViewController+CustomObject.m(分类实现)
@end
#import "UIViewController+CustomObject.h"
@implementation UIViewController (CustomObject)
- (void)customMethod
{
[self.customObject doSomething];
}
@end