16

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
  • 415,655
  • 72
  • 787
  • 1,044
HelenWang
  • 193
  • 2
  • 2
  • 10
  • Why are you synthesising and declaring your ivars? – hypercrypt Oct 21 '13 at 22:34
  • 1
    I might suggest watching WWDC 2012 video [Modern Objective-C](https://developer.apple.com/videos/wwdc/2012/?id=405), which talks about no longer needing to explicitly declare ivars that back your properties, as well as not even needing to explicitly `@synthesize` anymore. – Rob Oct 21 '13 at 23:28
  • @Rob,you mean to remove all declarations in interface and statements in synthesize? – HelenWang Oct 22 '13 at 02:40
  • 1
    The best practice is to not declare the backing ivars or use `@synthesize`. Just declare public `@property` variables in your `.h`, put your private ones in the class extension at the top of your `.m` file. No need to `@synthesize` anymore. – macshome Oct 22 '13 at 03:30
  • possible duplicate of [Autosynthesized property 'delegate' will use synthesized instance variable '\_delegate', not existing instance variable 'delegate'](http://stackoverflow.com/questions/14184691/autosynthesized-property-delegate-will-use-synthesized-instance-variable-del) – JOM Apr 29 '14 at 08:07

1 Answers1

23

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.

Yahel
  • 8,522
  • 2
  • 24
  • 32
Grady Player
  • 14,399
  • 2
  • 48
  • 76
  • 1
    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". – Rob Oct 21 '13 at 23:27
  • +1 for drawing a distinction between all new and all old school. – Aaron Brager Oct 21 '13 at 23:54
  • i think @Grady Player saying the one way is to remove all declarations under interface? – HelenWang Oct 22 '13 at 02:42
  • @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? – HelenWang Oct 22 '13 at 02:44
  • @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). – Rob Oct 22 '13 at 04:37
  • 1
    @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.) – Grady Player Oct 22 '13 at 15:21
  • @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). – Rob Oct 22 '13 at 15:28
  • I got it solved by removing "IBOutlet DataType identifier" from interface block in Header file. – Jayprakash Dubey Nov 19 '13 at 13:58