- I have 3 properties
id_1,id_2,id_3 id_2andid_3are derived fromid_1id_1can have public getter/setterid_2andid_3only havereadonlyaccess.
So I need to override the setter for id_1 to set id_2 and id_3 for valid id_1
id_1could come fromNSUserDefaultswhich means ininit, I need to setid_2andid_3So, I wanted to call setter of
id_1frominitas if I was calling from outside of the class usingivar_id_1That would give me a single implementation to set all the ids both during
initphase 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