简体   繁体   English

在iOS中合成CGFloat属性

[英]Synthesizing CGFloat property in iOS

I declared a property as below. 我宣布了一个属性如下。 From my readings on the web, it is not clear if I should also synthesize as below. 从我在网上的阅读中,我不清楚我是否也应该合成如下。 I have seen supporting blog posts for two different approaches. 我见过支持两种不同方法的博客文章。

@property (assign, nonatomic) CGFloat someFloat;

Then in the implementation: 然后在实施中:

@synthesize someFloat = _someFloat;

I have also seen in some cases: 在某些情况下我也看到过:

@synthesize someFloat;

From readings, I understand that "someFloat" is a property name, and "_someFloat" is created through the synthesis. 根据读数,我理解“someFloat”是属性名称,“_someFloat”是通过综合创建的。 So I am under the impression that the first way is correct. 所以我认为第一种方式是正确的。 However, I have used the second approach without problems. 但是,我使用第二种方法没有问题。 And I have seen the second approach in other code and blogs. 我在其他代码和博客中看到了第二种方法。

Can someone tell me what is the correct way and why? 有人能告诉我什么是正确的方法和原因?

In general, you no longer need to manually write @synthesize anymore. 通常,您不再需要手动编写@synthesize In your example, if you write 在你的例子中,如果你写

@property (assign, nonatomic) CGFloat someFloat;

The compiler will automatically synthesize it for you, which would be equivalent to you writing 编译器会自动为你合成它,这相当于你写的

@synthesize someFloat = _someFloat;

Hence, you would be able to access the property through self.someFloat or access the ivar within the implementation file by using _someFloat . 因此,你就可以通过访问属性self.someFloat或使用实现文件中访问伊娃_someFloat

If, however, you manually synthesize it like 但是,如果您手动合成它,就像

@synthesize someFloat;

The compiler automatically creates a backing ivar titled someFloat ... thereby, you would still be able to access the variable through the getter self.someFloat (that is, equivalent to [self someFloat] call). 编译器会自动创建一个名为someFloat的支持ivar ...因此,你仍然可以通过getter self.someFloat访问变量(也就是说,相当于[self someFloat]调用)。

Or, you could access the ivar by simply using someFloat somewhere within the implementation... 或者,你可以通过在实现中的某个地方使用someFloat来访问ivar ...

In general, it's not recommended to synthesize like this because it's quite easy to accidentally use the ivar when you meant to access the variable using the getter. 一般来说,不建议像这样合成,因为当你打算使用getter访问变量时,很容易意外地使用ivar。

EXCEPTION TO THE RULE 除了规则之外

The compiler still gets confused with synthesizing variables sometimes, however, in certain instances. 然而,在某些情况下,编译器有时会混淆合成变量。 For example, if you have a class that is a subclass of NSManagedObject , then you still need to write the @synthesize manually (assuming, of course, you actually want to synthesize the property... you likely don't though...). 例如,如果你有一个类是NSManagedObject的子NSManagedObject ,那么你仍然需要手动编写 @synthesize (当然,假设你实际上想要合成属性......你可能不会... )。

The reason for this is two-fold: (1) the compiler doesn't seem to understand these properties very well yet (or at least it doesn't in the cases I've worked with), and (2) many times, you actually DON'T want to @synthesize properties on an NSManagedObject subclass... rather, you want them to be @dynamic instead (so the getter/setter will be generated at runtime, per requirements of NSManagedObject subclass magic). 原因有两方面:(1)编译器似乎还没有很好地理解这些属性(或者至少在我使用的情况下没有),以及(2)很多次,你其实并不@synthesize上的性能NSManagedObject子......相反,你想他们是@dynamic而不是(这样的getter / setter将在运行时产生的,每个要求NSManagedObject子类魔法)。

只需跳过@synthesize语句,编译器将生成与您具有相同的内容:

@synthesize someFloat = _someFloat;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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