Objective C - Custom Getter with inheritance
- by anhdat
Recently I have worked with Core Data. When I want to set a default value for some fields, I came up with this problem:
So I made a simple represent:
We have 2 class Parent and Child, in which Child inherit from Parent.
// Parent.h
@interface Parent : NSObject
@property (strong, nonatomic) NSString *lastName;
// Child.h
@interface Child : Parent
In Parent class, I made a custom getter to set a default value when nothing is set:
// Parent.h
- (NSString *)lastName
{
if (_lastName) {
return _lastName;
} else {
return @"Parent Default Name";
}
}
But I cannot make a custom default value for the field "name" which Child inherits from its Parent.
// Child.h
@implementation Child
- (NSString *)lastname
{
if (super.lastName) {
return super.lastName;
} else {
return @"Child Default Name";
}
}
Apparently, this method is never called.
So my question here is: How can I set a custom getter for the field the Child class inherits from Parent without define an overriding property?