Objective C - Custom Getter with inheritance
Posted
by
anhdat
on Stack Overflow
See other posts from Stack Overflow
or by anhdat
Published on 2014-05-27T03:05:04Z
Indexed on
2014/05/27
3:27 UTC
Read the original article
Hit count: 158
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?
© Stack Overflow or respective owner