简体   繁体   English

使用方法和属性有什么区别?

[英]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.

相关问题 定义方法的属性时,+和-有什么区别? - What is the difference between + and - when defining a property of method? Restkit中的“属性”和“属性”有什么区别 - What's the difference between “attribute” and “property” in restkit Swift 中的属性和变量有什​​么区别? - What is the difference between a property and a variable in Swift? 后续委托方法调用之间有什么区别? - What is the difference between following delegate method call? CLLocationManager的startMonitoringSignificantLocationChanges()和startUpdatingLocation()方法有什么区别? - What is difference between startMonitoringSignificantLocationChanges() and startUpdatingLocation() method of CLLocationManager? UITableViewController中的View属性和TableView属性有什么区别? - What is the difference between the View property and TableView property in UITableViewController? 属性观察者和属性包装器之间的主要区别是什么? - What's the main difference between property observers and property wrappers? 读写属性和非原子分配属性有什么区别? - what is the difference between a readwrite property and a nonatomic assign property? 方法重载和方法重写Ios之间有什么区别 - What is the difference between method overloading and method overriding Ios 委托属性声明中'weak'和'assign'之间的区别是什么 - What's the difference between 'weak' and 'assign' in delegate property declaration
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM