Let's consider an application with highly customized or complex views.
We'll have a specific kind of view-controller sending methods to a specific kind of UIView, where the UIView is itself composed of a number of other views.
The view should have a rich, domain-specific interface, allowing the controller to act is a thin "glue" layer between it and a similarly rich model.
So we override our controller's view property as follows:
@interface PlaybackViewController : UIViewController<StageLayoutDelegate, ControlPanelDelegate>
{
NSMutableArray* _sections;
LightingMode _lightingMode;
}
@property (nonatomic, strong) PlaybackView* view; // <------ Specific type of view
#pragma mark - injected
@property (nonatomic, strong) id<OscClient> oscClient;
@property (nonatomic, strong) AbstractStageLayoutView* stageLayoutView;
@end
Ovverriding makes sense over defining another accessor, and I can just send messages to the specific type of UIView without having to cast.
Problem: The only problem is that it results in a compiler warning:
property type 'PlaybackView *' is incompatible with type 'UIView *' inherited from 'UIViewController'
. . and I like to build code that doesn't have any warnings. This way a valid warning doesn't get missed by being buried amongst other warnings.
Question:
The problem here is not not the override of the property, its using a forward declaration of the class type.
So this...
@class PlaybackView;
@interface PlaybackViewController : UIViewController
@property (nonatomic, strong) PlaybackView* view;
@end
will give you the mentioned warning because the compiler cannot know the inheritance hierarchy of PlaybackView
. UIViewController
has a contract to provide a UIView
from its view
property
Its telling you that it thinks PlaybackView
is not a UIView
The simple solution here is to use a #import
instead to give the compiler full knowledge of PlaybackView
...
#import "PlaybackView.h"
@interface PlaybackViewController : UIViewController
@property (nonatomic, strong) PlaybackView* view;
@end
alternatively (but really bad form as the PCH is an optimising feature and shouldn't manage dependancies ) is to add #import "PlaybackView.h"
to your projects PCH
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With