[英]Objective-C Base Class Property Custom Getter Not Being Called From Subclass
基类接口:
@interface Base : NSObject
@property (nonatomic, readonly, getter=getPriceForListing) double_t priceForListing;
@end
基类实现:
@implementation Base
-(double_t)getPriceForListing
{
if (self.listPriceLow > 0 && self.listPriceHigh > 0)
{
return self.listPriceLow;
}
else if (self.listPriceLow > 0)
{
return self.listPriceLow;
}
else if (self.listPriceHigh > 0)
{
return self.listPriceHigh;
}
else
{
return self.currentPrice;
}
}
@end
子类接口:
@interface Subclass : Base
@end
子类实现:
@implementation Subclass
@dynamic priceForListing;
@end
子类的使用方式:
Subclass *sub = [[Subclass alloc] init];
NSLog(@"%@", sub.priceForListing);
我在这里遇到的问题是sub.priceForListing
始终返回零,并且永远不会调用基类getter,至少没有断点在那里。
您priceForListing
“ getter”定义为getPriceForListing
但正尝试使用priceForListing
访问它。 只需省略自定义名称。
将“ getter”方法重命名为priceForListing
。
如果没有后备实例变量,则永远不会设置IOW,您可以将其指定为readonly
。
如评论中所述:删除: @dynamic priceForListing;
。
仅供参考:在Objective-C / Cocoa中,“ get”前缀是按惯例保留给通过引用返回值的方法的。 吸气剂没有“ get”前缀。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.