fuzzygoat
fuzzygoat

Reputation: 26223

Calling @property accessor?

I wonder if someone can explain the following, are these both doing the same? As both call "setSeparatorColor" I would guess they are both calling the accessor for the property.

[myTableView setSeparatorColor:[UIColor orangeColor]];

.

[[self myTableView] setSeparatorColor:[UIColor orangeColor]];

Gary.

Upvotes: 0

Views: 105

Answers (2)

kpower
kpower

Reputation: 3891

Not quite the same.

In the first version you use instance variable of some class - myTableView.

In the second version you use value, returned by same-named method. On the first step current class's method - (..)myTableView; is called This method returns some value. And on the next step - you use - (..)setSeparatorColor:.. method of returned object. Of course, often (when you use @synthesize myTableView; or method implementation like - (..)myTableView { return myTableView; }) it is the same variable as in the first version, but it isn't mandatory condition (depends on your implementation). Moreover, - (..)myTableView; can have some side effects / do additional work - not just returning a value.

An example (myTableView and [self myTableView] can be different, depending on some condition):

// myClass.h
@interface myClass : UIViewController {
    UITableView *myTableView;
}
@property (nonatomic, retain) UITableView *myTableView;
@end;

// myClass.m
#import "myClass.h"

@implementation myClass

@dynamic myTableView;

- (UITableView *)myTableView {
    return (someConditionIsTrue) ? myTableView : nil;
}

- (void)setMyTableView:(UITableView *)value {
    if (myTableView != value) {
        [myTableView release];
        myTableView = [value retain];
    }
}

@end;

Upvotes: 3

Liam
Liam

Reputation: 8102

Correct, they are both doing the same

[myTableView setSeparatorColor:[UIColor orangeColor]];

Is accessing the variable directly, where as

[[self myTableView] setSeparatorColor:[UIColor orangeColor]];

Is calling the accessor for the property, and then sending the setSeparatorColor message to it

Upvotes: -1

Related Questions