1

I'm relatively new to ARC. I'm making an UIView subclass, that will have two labels (title and subtitle). I don't want to publicly expose the labels as properties, only their text.

I'm currently using this:

@interface MyView : UIView
@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSString *subtitle;
@end

 

@implementation MyView
{
    UILabel *_titleLabel;
    UILabel *_subtitleLabel;
}

- (void)setTitle:(NSString *)title
{
    [_titleLabel setText:title];
}

- (NSString *)title
{
    return [_titleLabel text];
}

- (void)setSubtitle:(NSString *)subtitle
{
    [_subtitleLabel setText:title];
}

- (NSString *)subtitle
{
    return [_subtitleLabel text];
}

@end

Are my two @properties correctly declared? Should I use the strong, weak or any other qualifier? And why?

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
Cyrille
  • 25,014
  • 12
  • 67
  • 90
  • @trojanfoe: this is not my point. See http://stackoverflow.com/questions/6785765/instance-variables-declared-in-objc-implementation-file – Cyrille Jan 30 '13 at 14:27
  • 2
    Actually it is legal. That's the new-ish way to declare private ivars. – Jack Lawrence Jan 30 '13 at 14:27
  • Just imagine I've declared them in a private category at the top of the `.m`. It doesn't change my question. – Cyrille Jan 30 '13 at 14:28
  • If that's legal then that's great; but I don't see why you need the properties at all as you aren't storing their value locally; just within the `UILabel`s. Why not just declare the setter/getters instead? – trojanfoe Jan 30 '13 at 14:32
  • Just because I want to be able to do `myView.title = @"Hello"; myView.subtitle = @"World"`. (And I wanna learn ARC, too, obviously.) – Cyrille Jan 30 '13 at 14:33
  • 1
    AFAIK that is ok. The only point is that I do not see the alloc/init of the UILabel objects. If they are properly created, e.g. within the init method of the view, then this should be fine. – Hermann Klecker Jan 30 '13 at 14:36
  • @HermannKlecker I've skipped the alloc/init for readability here. Of course, I instanciate them and lay them out at the proper places. – Cyrille Jan 30 '13 at 14:41

1 Answers1

1

If you are going to work with setter / getter, I think the appropiate tag would be the readwrite. strong weak retain etc apply when the property is the setter/getter for an instance variable.

Ismael
  • 3,927
  • 3
  • 15
  • 23
  • 1
    So, no qualifier at all? I think I remember `readwrite` is the default. – Cyrille Jan 30 '13 at 14:30
  • Yeah, that's about it really. When you implement the getter/setters and you are not even storing the value in an instance variable, it doesn't really matter, specially when using ARC – Ismael Jan 30 '13 at 15:07
  • Agreed. That sounds logical. – Cyrille Jan 30 '13 at 15:11
  • Confirmed to work. The key is effectively "no instance variable --> no lifetime/ownership qualifier". (The two labels are ivars, so by default, they're strongly owned by the class, but never exposed publicly). – Cyrille Jan 30 '13 at 15:15