简体   繁体   English

是否有更快的方法来执行@property,@synthesize并释放类变量?

[英]Is there a faster way to do @property , @synthesize and release a class variable?

I know @synthesize has already reduced lot of my work of writing getters and setters. 我知道@synthesize已经减少了我编写getter和setter的工作量。 But one common procedure I often have to use are these 4 steps for example 但是我经常使用的一个常见程序是这四个步骤

  1. SomeView *abc;
  2. @property(nonatomic,retain)SomeView *abc;
  3. @synthesize abc;
  4. [abc release];

Has someone come up with any idea where I write SomeView *abc ; 有人想出我在哪里写SomeView *abc ; in .h and steps 2,3,4 are generated automatically? .h和步骤2,3,4自动生成?

跳过第一步,没有必要,@synthesize将创建ivar。

If it's an IBOutlet , Xcode will write all that code for you when you create the variable using IB. 如果它是IBOutlet ,Xcode将在您使用IB创建变量时为您编写所有代码。 You just drag from the object you want to reference in the interface editor to the location in the header file where you want the property declaration, fill out a form, and hit OK. 您只需将要在界面编辑器中引用的对象拖动到头文件中要进行属性声明的位置,填写表单,然后单击“确定”。

As others have mentioned, you can skip declaring the backing ivar in favor of having the @synthesize generate it for you. 正如其他人所提到的,你可以跳过声明支持ivar,转而让@synthesize为你生成它。

You can skip the @synthesize by using the appropriate compiler flags. 您可以使用适当的编译器标志跳过@synthesize Search the web for something like "default @synthesize". 在网络上搜索“default @synthesize”之类的内容。

One way to skip -dealloc is to dangle the objects off your main object using the Obj-C associated object API. 跳过-dealloc一种方法是使用Obj-C关联对象API将对象-dealloc在主对象之外。 Retained associated objects will be released when the object they are associated with is released. 保留关联对象将在与其关联的对象被释放时释放。

And then there's Automatic Reference Counting (ARC), which eliminates -dealloc much more cleanly and definitively. 然后是自动引用计数(ARC),它可以更加干净和明确地消除-dealloc

It's not a lot of help, but one thing I do is move dealloc to the top of the implementation, ahead of other methods. 这不是很多帮助,但我做的一件事就是将dealloc移到实现的顶端,领先于其他方法。 The @synthesize statements are right above that, so you can put in the @synthesize and the release often without having to scroll. @synthesize语句就在它之上,所以你可以经常放入@synthesize和release,而不必滚动。 It's not really a big time-saver when coding, but it does help you keep the two sections in sync, and that's a time-saver when debugging. 在编码时,这并不是一个真正的节省时间,但它确实可以帮助您保持两个部分同步,这在调试时节省了时间。

It's not a bad thing to wish for. 希望这不是一件坏事。 And it's done for you already in some cases: 在某些情况下,它已经为你完成了:

In Xcode 4's Interface Builder when you drag an element onto it's file's owner's .h file it does this 1-4 for you, and even sets it to nil in viewDidUnload for you. 在Xcode 4的Interface Builder中,当你将一个元素拖到它的文件所有者的.h文件上时,它会为你做1-4,甚至在viewDidUnload中将它设置为nil。

  1. Not needed with the modern runtime. 现代运行时不需要。
  2. Still need to do this. 仍然需要这样做。
  3. Doing away with this was talked about in the WWDC10 (113 and 144) sessions, but the release notes say that this hasn't happened yet. 在WWDC10(113和144)会议上讨论了废除这一点,但发布说明说这还没有发生。
  4. Use ARC, and you don't need this. 使用ARC,您不需要这个。

That's 2 of the 4 that you don't need to do, and one that they are working on removing. 这是你不需要做的4个中的2个,以及他们正在努力去除的4个。 So it's not all there yet - but it is getting easier. 所以它还不是全部 - 但它变得越来越容易。

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

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