【问题】
折腾Picker View的过程中,遇到变量使用出错:
【解决过程】
1.后来经过折腾,发现是 property和instance value之间的区别没有搞清楚,
解决办法是:
(1)要么去interface中定义实例变量:
1 2 3 4 5 6 7 8 9 10 | // // AddSightingViewController.h // BirdWatching // @interface AddSightingViewController : UITableViewController <UITextFieldDelegate, UIPickerViewDataSource, UIPickerViewDelegate> { //NSArray *birdNameList; } @property (strong, nonatomic) NSArray *birdNameList; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | // // AddSightingViewController.m // BirdWatching // @implementation AddSightingViewController //@synthesize birdNameList = _birdNameList; - ( void )viewDidLoad { [super viewDidLoad]; self.birdNameList = [[NSArray alloc] initWithObjects:@ "Ostrich" , @ "Penguin" , @ "HummingBird" , nil]; } |
(2)要么去使用property属性变量:
1 2 3 4 5 6 7 8 9 10 | // // AddSightingViewController.h // BirdWatching // @interface AddSightingViewController : UITableViewController <UITextFieldDelegate, UIPickerViewDataSource, UIPickerViewDelegate> { NSArray *birdNameList; } //@property (strong, nonatomic) NSArray *birdNameList; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | // // 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