- I have 3 properties
id_1
,id_2
,id_3
id_2
andid_3
are derived fromid_1
id_1
can have public getter/setterid_2
andid_3
only havereadonly
access.
So I need to override the setter for id_1
to set id_2
and id_3
for valid id_1
id_1
could come fromNSUserDefaults
which means ininit
, I need to setid_2
andid_3
So, I wanted to call setter of
id_1
frominit
as if I was calling from outside of the class usingivar
_id_1
That would give me a single implementation to set all the ids both during
init
phase or if called externally
My question is on following two lines that I have in my code as I am calling the setter for id_1
with argument as ivar _id_1
_id_1 = id_from_ns_user_defaults
[self setid_1:_id_1];
In few other SO articles I saw concerns around recursive loops
.h file
@interface UserCredentials : NSObject
@property (strong, nonatomic) NSString *id_1;
@property (readonly) NSString *id_2;
@property (readonly) NSString *id_3;
@end
.m file
@interface UserCredentials ()
@property (readwrite) NSString *id_2;
@property (readwrite) NSString *id_3;
@end
@implementation UserCredentials
- (id)init
{
self = [super init];
if (self) {
/* Is this valid in Objective-C */
_id_1 = id_from_ns_user_defaults
[self setid_1:_id_1];
}
return self;
}
- (void)setid_1:(NSString *)id
{
if (id && ![id isEqualToString:@""]) {
_id_1 = id;
_id_2 = convert2(_id_1);
_id_3 = convert3(_id_1);
}
}
@end