[英]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: 我正在研究“ The Elements”示例应用程序的源代码,并且看到在AtomicElement.h中有四个声明为只读的属性:
@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];
在头文件中,然后像
[element stateImageForAtomicElementTileView];
一样访问它[element stateImageForAtomicElementTileView];
instead of element.stateImageForAtomicElementTileView
? 而不是
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
. 访问而不是
[ ]
作为记录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) 这是类的一部分的图像(事实不是从资源中即时生成的,应将其视为实现细节)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.