Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override UIViewController.view with specific type

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:

  • Is there a way to suppress this particular warning?
  • Why is this part of the default settings, when most modern OO languages will happily allow overriding a property or method in a sub-class so that it returns a more specific sub-class of the type declared in the super-class?
like image 957
Jasper Blues Avatar asked Feb 05 '13 01:02

Jasper Blues


1 Answers

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

like image 155
Warren Burton Avatar answered Sep 21 '22 12:09

Warren Burton