Frage

Let's assume there is a UIView that contains a UILabel internally.

The UIView's public interface is as follows in it's .h:

@interface MyView : UIView

@property (nonatomic, copy) NSString *text;

@end

and privately within it's .m:

@interface MyView ()

@property (nonatomic, strong) UILabel coolLabel;

@end

with the implementation in it's .m:

@implementation

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

- (NSString *)text
{
   return self.coolLabel.text;
}

@end

Looking at the public interface I declare @property (nonatomic, copy) NSString *text with a copy because the text is copied by the coolLabel internally. However, I'm not sure if this is correct.

Should the declaration be @property (nonatomic, assign) NSString *text with an assign instead because my class isn't specifically performing the memory management? Or is my current interface declared correctly?

FYI, everything assumes ARC

War es hilfreich?

Lösung

Your text property should be copy because ultimately its value is being copied by the label.

The property definition is like a contract. You are telling users of the property how the value will be treated. Since the label copies the text, you should indicate your property is being copied.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top