[英]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.