0

I get the following warning/error in xcode:

Autosynthesized property 'view' will use synthesized instance variable '_view', not existing instance variable 'view'

when I was trying to redefine/override the class of the view property on my NSViewController.

@interface DABListViewController : NSViewController
  @property (nonatomic, strong) DABListViewControllerView *view;
@end

Also, the view doesn't seem to appear. This question looks similar but didn't work.

Community
  • 1
  • 1
waggles
  • 2,804
  • 1
  • 15
  • 16
  • 1
    `NSViewController` already has a property called `view`. You can't override that property by declaring another property with the same name. What you can do is 1) create an instance of `DABListViewController` 2) alloc/init a `DABListViewControllerView` as a local variable 3) call the view controller's `setView:` method to change the view. Either that, or just use the Identity inspector in Interface Builder to change the class of the view. – user3386109 May 22 '14 at 20:28
  • I see thanks. Can this comment be an answer? I don't feel like my question is a duplicate of either of the questions it's marked a duplicate of. This comment answers my question but conflicts the [answer I found here](http://stackoverflow.com/a/16423616/1252895) as far as I can tell. – waggles May 23 '14 at 01:57
  • After a question gets closed, it's not possible to add answers. That's why I posted as a comment. FWIW, I agree that your question is not a duplicate of the ones they chose. – user3386109 May 23 '14 at 04:51

1 Answers1

-3

Including the @dynamic view; in my implementation file fixed the problem.

@implementation TSPollListViewController

  @dynamic view;

  - (void)loadView {
    self.view = [[DABListViewControllerView alloc] init];
  }

@end

The @dynamic tells the compiler not worry about the properties because they'll be taken care of at run time. I guess in telling the compiler to ignore the property we don't override the view instance variable defined in NSViewController. I think this also means there is no _view. This should work with UIViewController and most properties you want to redefine the Type for in subclasses.

waggles
  • 2,804
  • 1
  • 15
  • 16
  • @dynamic isn't the correct solution... see: http://stackoverflow.com/questions/19505995/how-to-fix-the-warning-of-autosynthesized-property-myvar-will-use-synthesized/19506189#19506189 – Grady Player May 22 '14 at 19:18
  • 1
    To re-emphasize; **DO NOT DO THIS!** Totally the wrong "fix"; not a fix at all. – bbum May 22 '14 at 20:25
  • Hmm, sorry @bbum. So this [answer](http://stackoverflow.com/a/16423616/1252895) is also wrong and misleading or is there a different situation there? I thought I was essentially doing what was described there, but when I left out the `@dynamic` I got the warning. – waggles May 23 '14 at 01:32
  • [Travis Jeffery in his blog](http://travisjeffery.com/b/2012/12/overriding-uiviewcontrollers-view-property-done-right/) has highlighted exactly this solution as absolutely correct. Can anyone explain where is the truth? – nalexn May 23 '14 at 05:06
  • @waggles It doesn't fix anything, it just silences the warning for unrelated reasons. You'll still end up with two instance variables -- `view` and `_view` -- and much confusion will ensue the first time you use the wrong one. – bbum May 23 '14 at 05:07
  • 1
    @bbum; As far as I know about @dynamic, it just prevents autogeneration of accessory methods, so the subclass will use superclass's setView: and view methods, so everything there should be fine, or not? – nalexn May 23 '14 at 05:23
  • @NAlexN Derp (Wow. derpity-derp, in fact). Yes, correct. Thanks. Fixed: Travis's weblog post doesn't actually explain why he used `@dynamic`. It relies on passing through to super; it also relies on co-variant declarations, which is explicitly verboten in Objective-C, but is coincidentally safe in this case. – bbum May 23 '14 at 05:26