How to fix the warning of "Autosynthesized property 'myVar' will use synthesized instance variable '_myVar', not existing instance variable 'myVar' "?

11,674

Edit: Basically for all intents and purposes you should be building on a new enough XCode to use the new behavior, in that solution you typically will just remove the ivar from the @interface block in the .h file... If for some reason you do need to access an ivar directly you can now declare it in the @implementation block... and use @synthesize var or @synthesize var=_var

OGPost:

to make that go away you can go all new school and drop the iVar, or you can go all old school and add an @synthesize someIvar in your @implementation block.

Share:
11,674
HelenWang
Author by

HelenWang

A new coder. Work Hard :)

Updated on July 20, 2022

Comments

  • HelenWang
    HelenWang almost 2 years

    I declare my .h file like this:

    #import <UIKit/UIKit.h>
    
    @interface NavigationTripViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>{
        NSArray *questionTitleTrip;
        NSArray *questionDescTrip;
        NSMutableArray *answerTrip;
        NSMutableArray *pickerChoices;
        int questionInt;
        int totalInt;
        IBOutlet UILabel *questionNum;
        IBOutlet UILabel *questionTotalNum;
        IBOutlet UILabel *recordType;
        IBOutlet UITextView *questionDes;
        IBOutlet UIView *answerView;
        IBOutlet UIButton *preButton;
        IBOutlet UIButton *nextButton;
        UITextField *text;
        UIPickerView *picker;
    
    }
    @property (retain, nonatomic) NSArray *questionTitleTrip;
    @property (retain, nonatomic) NSArray *questionDescTrip;
    @property (retain, nonatomic) NSMutableArray *answerTrip;
    @property (retain, nonatomic) NSMutableArray *pickerChoices;
    @property (retain, nonatomic) IBOutlet UILabel *questionNum;
    @property (retain, nonatomic) IBOutlet UILabel *questionTotalNum;
    @property (retain, nonatomic) IBOutlet UILabel *recordType;
    @property (retain, nonatomic) IBOutlet UITextView *questionDes;
    @property (retain, nonatomic) IBOutlet UIView *answerView;
    @property (retain, nonatomic) IBOutlet UIButton *preButton;
    @property (retain, nonatomic) IBOutlet UIButton *nextButton;
    @property (retain, nonatomic) UITextField *text;
    @property (retain, nonatomic) UIPickerView *picker;
    -(IBAction)clickPre;
    -(IBAction)clickNext;
    @end
    

    And my .m file here like this:

    #import "NavigationTripViewController.h"
    #import <QuartzCore/QuartzCore.h>
    
    @interface NavigationTripViewController ()
    
    @end
    
    @implementation NavigationTripViewController
    
    @synthesize questionTitleTrip,questionDescTrip,answerTripl,pickerChoices,questionNum,questionTotalNum,recordType,questionDes,answerView,preButton,nextButton,text,picker;
    

    All my variables in the @synthesize receive the warnings:

    Autosynthesized property 'myVar' will use synthesized instance variable '_myVar', not existing instance variable 'myVar'

    Also, the variables and class names used in viewDidLoad don't display in colors, just show in black color. In other view controllers, there are no warnings like this. How to fix these problems?

  • Rob
    Rob over 10 years
    Helen should probably should do the former, as that's generally considered best practice nowadays. If doing the latter, she may want to turn on the "Implicit Synthesized Variables" warning under "Build Settings".
  • Aaron Brager
    Aaron Brager over 10 years
    +1 for drawing a distinction between all new and all old school.
  • HelenWang
    HelenWang over 10 years
    i think @Grady Player saying the one way is to remove all declarations under interface?
  • HelenWang
    HelenWang over 10 years
    @Rob, is this a new change? You mean if i want to do this way, i should go to the build settings to make "Implicit Synthesized Variable" Yes?
  • Rob
    Rob over 10 years
    @HelenWang Correct, if you're going to explicitly @synthesize, you should use that warning to make sure you don't accidentally forget to do so. But frankly, you should probably never @synthesize (and never manually create the ivar used to back the property).
  • Grady Player
    Grady Player over 10 years
    @Rob "Never" is a little strong, some of us have to build on systems that have an Xcode that is pre 4.5(?), or whenever that feature was introduced, but it is true that if you don't know why you would choose one or another, then you should let the compiler do the heavy lifting for you... I like explicitly declaring my ivars, because it gives me a good checklist of objects to release in my dealloc (some of us don't get to support ARC either.)
  • Rob
    Rob over 10 years
    @GradyPlayer Agreed, "never" is strong. If you need backward compatibility (e.g. building a class that users still using old compilers might use), then do so, but turn on that warning if you do manually instantiate, because you're introducing the risk that a minor typo might accidentally lead to two ivars (and I've answered more than one question on S,O. where this was the source of problems).
  • Jayprakash Dubey
    Jayprakash Dubey over 10 years
    I got it solved by removing "IBOutlet DataType identifier" from interface block in Header file.