Ivars を宣言せずにスーパークラスで a を宣言@property
し、それをサブクラス化し、サブクラスでスーパークラス ivar() を使用して getter を実装しようとすると_propertyName
、xcode は を示すエラーを呼び出しますUse of undeclared identifier '_propertyName'
。
ベスト プログラミング プラクティスに準拠するソリューションは何ですか?
私@synthesize propertyName = _propertyName
は@implementation
サブクラスの中にいるべきですか、それとも
@interface SuperClass : AnotherClass
{
Type *_propertyName;
}
@property Type *propertyName;
@end
編集:
プロパティのアクセサーメソッドの自動「合成」と、コンパイラーによる「アンダーバー ivar」の作成を理解しています。 ivar は、インターフェイスまたは実装セクションでの ivar の宣言または宣言 なし
で、の実装からアクセスできます。SuperClass
@synthesize
私のケースのさらなる説明: 免責事項: アルフィー・ハンセンから盗まれたコードのブロックの内容
@interface SuperViewController : UIViewController
@property (nonatomic, strong) UITableView * tableView; // ivar _tableView is automatically @synthesized
@end
#import "SuperViewController.h"
@interface SubViewController : SuperViewController
// Empty
@end
@implementation SubViewController
- (void)viewDidLoad
{
NSLog(@"tableView: %@", self.tableView); // this is perfectly OK
}
// ************* This causes problem **************
- (UITableView *) tableView {
if (!_tableView) { // Xcode error: Use of undeclared identifier '_propertyName'
_tableView = [[SubclassOfUITableView alloc] init];
}
return _tableView;
}
// ************************************************
@end