[英]Objective-C Base Class Property Custom Getter Not Being Called From Subclass
Base Class Interface: 基类接口:
@interface Base : NSObject
@property (nonatomic, readonly, getter=getPriceForListing) double_t priceForListing;
@end
Base Class Implementation: 基类实现:
@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
Subclass Interface: 子类接口:
@interface Subclass : Base
@end
Subclass Implementation: 子类实现:
@implementation Subclass
@dynamic priceForListing;
@end
How subclass is used: 子类的使用方式:
Subclass *sub = [[Subclass alloc] init];
NSLog(@"%@", sub.priceForListing);
The problem I am having here is that sub.priceForListing
always returns zero and the base class getter is never being called, at least no breakpoint is being hit in there. 我在这里遇到的问题是sub.priceForListing
始终返回零,并且永远不会调用基类getter,至少没有断点在那里。
You defined the "getter" as getPriceForListing
but are trying to access it with priceForListing
. 您priceForListing
“ getter”定义为getPriceForListing
但正尝试使用priceForListing
访问它。 Just omit the custom name. 只需省略自定义名称。
Rename the "getter" method to priceForListing
. 将“ getter”方法重命名为priceForListing
。
If there is no backing instance variable, IOW it is never set you can specify it to be readonly
. 如果没有后备实例变量,则永远不会设置IOW,您可以将其指定为readonly
。
As mentioned in the comments: Delete: @dynamic priceForListing;
如评论中所述:删除: @dynamic priceForListing;
. 。
FYI: In Objective-C/Cocoa the "get" prefix is reserved by convention for methods that return a value by reference. 仅供参考:在Objective-C / Cocoa中,“ get”前缀是按惯例保留给通过引用返回值的方法的。 Getters do not have a "get" prefix. 吸气剂没有“ get”前缀。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.