简体   繁体   中英

What is the difference between using a method and a property?

I am studying the source code for "The Elements" sample app and I see that in AtomicElement.h there are four properties declared as readonly:

@property (readonly) UIImage *stateImageForAtomicElementTileView;
@property (readonly) UIImage *flipperImageForAtomicElementNavigationItem;
@property (readonly) UIImage *stateImageForAtomicElementView;
@property (readonly) CGPoint positionForElement;

In the implementation file, they look like

- (UIImage *)stateImageForAtomicElementTileView {
    return [UIImage imageNamed:[NSString stringWithFormat:@"%@_37.png",state]];
}

Can you please elaborate on the reasons to do that? Why not use something like

- (UIImage*) stateImageForAtomicElementTileView;

in the header file, and then access it like [element stateImageForAtomicElementTileView]; instead of element.stateImageForAtomicElementTileView ?

I don't think there's a technical reason to choose one idiom over the other, they can both be used in the same way. It's more a matter of semantics.

A class has both data and can perform operations (usually on said data). I think you should look at using a property, and having the . access instead of [ ] more as a way of documenting the purpose of the stateImageForAtomicElementTileView .

It's an image that's part of the class (the fact is't being generated on the fly from a resource should be seen as an implementation detail)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM