开发者

Calling @property accessor?

开发者 https://www.devze.com 2023-01-09 07:26 出处:网络
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.

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.


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;


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号