0
  • I have 3 properties id_1, id_2, id_3
  • id_2 and id_3 are derived from id_1
  • id_1 can have public getter/setter
  • id_2 and id_3 only have readonly 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 from NSUserDefaults which means in init, I need to set id_2 and id_3
  • So, I wanted to call setter of id_1 from init as if I was calling from outside of the class using ivar _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

Custom Getter & Setter iOS 5

.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
Community
  • 1
  • 1
GJain
  • 5,025
  • 6
  • 48
  • 82
  • If `id_2` and `id_3` are derived from `id_1`, why should you store their values at all? Why not just implement getters that compute the value when needed? – mspensieri Mar 02 '15 at 19:24
  • I think I have an expensive computation and other uses....but was just wanted to understand if its ok to do what I posted – GJain Mar 02 '15 at 19:26
  • @michaels..thanks for pointing that out....I was not thinking in that direction... – GJain Mar 02 '15 at 19:46

1 Answers1

0

Your highlighted concern is around creating an assignment cycle. Because you are assigning to the ivar itself, you will not be creating a cycle. Remember that manipulating the ivar will not cause your getter/setter to be called -- it's just a pointer like any other pointer.


Setting an ivar to itself is not an issue unless you have done something in your setter implementation to make it an issue. In non-ARC systems, you could easily create a bad access error by implementing your setter with the wrong order:

- (void)setVal:(NSObject *)val {
  [_val release];
  _val = [val retain];
}

This is countered by using autorelease instead (or assigning to a temporary variable and releasing after the retain).

Most of the time, though, your setter won't be doing anything destructive when passed a new (or same) value. Your implementation does not do this.

Ian MacDonald
  • 13,472
  • 2
  • 30
  • 51