views:

45

answers:

2

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.

A: 

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

Liam
No, they are not the same. The first one accesses the instance variable directly.
JeremyP
Isn't that what I said?
Liam
+1  A: 

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;
kpower