小编use*_*382的帖子

在超类中声明ivars或在子类中声明@synthesize?

当我@property在没有声明ivars的情况下声明一个超类中的子类时,将其子类化并尝试使用_propertyName子类中的超类ivar()实现getter ,xcode调用一个错误说明Use of undeclared identifier '_propertyName'.

什么是符合最佳编程实践的解决方案?

我应该@synthesize propertyName = _propertyName@implementation子类中还是

@interface SuperClass : AnotherClass
{
    Type *_propertyName;
}

@property Type *propertyName;

@end
Run Code Online (Sandbox Code Playgroud)

编辑:
我确实理解属性的访问器方法的自动"综合"和编译器创建的"underbar ivars".
可以通过 接口或实现部分中SuperClass没有任何@synthesizeivars声明或声明来实现ivar .

进一步澄清我的案例:免责声明:内容被窃取的Alfie Hanssen代码块

@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); // …
Run Code Online (Sandbox Code Playgroud)

properties subclass objective-c

5
推荐指数
1
解决办法
4365
查看次数

标签 统计

objective-c ×1

properties ×1

subclass ×1