最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】iOS的Xcode中错误:Use of undeclared identifier xxx did you mean _xxx

iOS crifan 9962浏览 0评论

【问题】

折腾Picker View的过程中,遇到变量使用出错:

Use of undeclared identifier xxx did you mean _xxx

 

【解决过程】

1.后来经过折腾,发现是 property和instance value之间的区别没有搞清楚,

解决办法是:

(1)要么去interface中定义实例变量:

//
//  AddSightingViewController.h
//  BirdWatching
//
@interface AddSightingViewController : UITableViewController <UITextFieldDelegate, UIPickerViewDataSource, UIPickerViewDelegate>
{
    //NSArray *birdNameList;
}

@property (strong, nonatomic) NSArray *birdNameList;

 

//
//  AddSightingViewController.m
//  BirdWatching
//


@implementation AddSightingViewController

//@synthesize birdNameList = _birdNameList;


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.birdNameList = [[NSArray alloc] initWithObjects:@"Ostrich", @"Penguin", @"HummingBird", nil];
}

 

(2)要么去使用property属性变量:

//
//  AddSightingViewController.h
//  BirdWatching
//
@interface AddSightingViewController : UITableViewController <UITextFieldDelegate, UIPickerViewDataSource, UIPickerViewDelegate>
{
    NSArray *birdNameList;
}

//@property (strong, nonatomic) NSArray *birdNameList;

 

 

//
//  AddSightingViewController.m
//  BirdWatching
//


@implementation AddSightingViewController

//@synthesize birdNameList = _birdNameList;


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    birdNameList = [[NSArray alloc] initWithObjects:@"Ostrich", @"Penguin", @"HummingBird", nil];
}

这样,至少就解决了编译的错误问题了。

 

而关于两者之间的区别,详情参考:

【整理】Object-C中属性(property)与实例变量(instance variable)之间的区别

转载请注明:在路上 » 【已解决】iOS的Xcode中错误:Use of undeclared identifier xxx did you mean _xxx

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.139 seconds, using 22.12MB memory