iosobjective-cmemory-managementivars

Does Using Assign reduce the amount of memory used?


When I use assign when declaring a synthesized propery, does ARC automatically still create a matching ivar to it? My property is as follows

@property (nonatomic, assign) NSString *text:

And

- (NSString *)text {
    return self.label.text; // label is a UILabel
}

- (void)setText:(NSString *)text {
    self.label.text = text;
}

I never have any use for the automatically generated _text ivar; does the compiler still create this ivar when I omit @synthesize text = _text or does the unused ivar just persist in the memory unused?


Solution

  • Do not use assign this way. It probably won't matter in this particular case, but it's extremely confusing to the caller, and it'll generate very bad bugs if you ever change the implementation.

    The fact that you implemented the getter and setter means that the compiler won't generate an ivar. That has nothing to do with what memory-management attribute you use. Use strong here because that's what you implemented. Your header should match your implementation.