【问题标题】:How to fix the warning of "Autosynthesized property 'myVar' will use synthesized instance variable '_myVar', not existing instance variable 'myVar' "?如何修复“自动合成属性'myVar'将使用合成实例变量'_myVar',而不是现有实例变量'myVar'”的警告?
【发布时间】:2013-10-21 22:32:21
【问题描述】:

我这样声明我的 .h 文件:

#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

我的 .m 文件在这里是这样的:

#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;

@synthesize 中的所有变量都会收到警告:

自动合成的属性“myVar”将使用合成的实例变量“_myVar”,而不是现有的实例变量“myVar”

另外,viewDidLoad 中使用的变量和类名不显示为彩色,仅显示为黑色。在其他视图控制器中,没有这样的警告。如何解决这些问题?

【问题讨论】:

标签: ios objective-c properties


【解决方案1】:

编辑: 基本上出于所有意图和目的,您应该在足够新的 XCode 上构建以使用新行为,在该解决方案中,您通常只需从 .h 文件中的 @interface 块中删除 ivar ... 如果出于某种原因您确实需要直接访问 ivar 您现在可以在 @implementation 块中声明它...并使用 @synthesize var@synthesize var=_var

OGPost:

要消除这种情况,您可以去所有新学校并放弃 iVar,或者您可以去所有旧学校并在您的 @implementation 块中添加 @synthesize someIvar

【讨论】:

  • Helen 可能应该做前者,因为这通常被认为是当今的最佳实践。如果是后者,她可能想打开“构建设置”下的“隐式合成变量”警告。
  • +1 用于区分所有新学校和所有旧学校。
  • 我认为@Grady Player 说的一种方法是删除接口下的所有声明?
  • @Rob,这是新变化吗?你的意思是如果我想这样做,我应该去构建设置来制作“隐式合成变量”是吗?
  • @Rob "Never" 有点强,我们中的一些人必须在具有 4.5(?) 之前的 Xcode 的系统上构建,或者无论何时引入该功能,但确实如果您不知道为什么要选择一个或另一个,那么您应该让编译器为您完成繁重的工作...我喜欢明确声明我的 ivars,因为它为我提供了一个很好的对象清单以在我的 dealloc 中释放(我们中的一些人也不支持 ARC。)
猜你喜欢
  • 2014-07-11
  • 2011-05-29
  • 1970-01-01
  • 2014-10-22
  • 2012-03-03
  • 2014-04-08
  • 2013-03-01
  • 1970-01-01
  • 2018-05-13
相关资源
最近更新 更多