XCode iPhone 自动属性、合成和 delloc

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5387587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:36:29  来源:igfitidea点击:

XCode iPhone automatic property, synthesize and delloc

iphonexcoderefactoring

提问by necixy

I have been developing for iPhone from last 1-2 months and all the time for taking IBOutlet I use the following method to declare any property:

我在过去 1-2 个月一直在为 iPhone 开发,并且一直在使用 IBOutlet 我使用以下方法来声明任何属性:

In .h files:

在 .h 文件中:

@interface ....
{
     controlType varName;
}

@property() IBOutlet controlType varName;

In .m files:

在 .m 文件中:

at top -

在顶部 -

@synthesize varName;

in delloc method - [varName release];

Although the method is good but it isn't very good when its taking a couple of minutes just for declaring property.

方法虽然不错,但是只花几分钟的时间来声明属性就不是很好了。

Is there any automatic process for declaring properties, IBOutlets, IBActions or basic data variables.

是否有任何用于声明属性、IBOutlets、IBActions 或基本数据变量的自动过程。

I hope there should be some automatic methods for this scenario as this is a very basic process.

我希望这个场景应该有一些自动方法,因为这是一个非常基本的过程。

Thanks all.

谢谢大家。

采纳答案by Fattie

In fact, yes!

事实上,是的!

You no longer need to declare the actual iVar.

您不再需要声明实际的 iVar。

In short, simply leave out this part: controlType varName;

简而言之,只需省略这部分: controlType varName;

For now, you do still need to explicitly "synthesize".

目前,您仍然需要明确地“综合”。

(As far as I can see, they could possibly automate that in the future. But for now you have to "synthesize" to create the setter and getter.)

(据我所知,他们将来可能会自动化。但现在你必须“综合”来创建 setter 和 getter。)

You do still have to release the memory - there's really no way that could be automated, as memory handling is "real he-man programming."

您仍然必须释放内存 - 真的没有办法可以自动化,因为内存处理是“真正的人类编程”。

For any new chums reading, don't forget the "self." part when using the property.

对于任何新朋友阅读,不要忘记“自我”。使用该属性时的一部分。

Plus note that XCode4 even has a new automatic thingy. Simply drag from the interface builder, to your .h file, and it will do everything for you -- try it!

另外请注意,XCode4 甚至还有一个新的自动事物。 只需从界面构建器拖动到您的 .h 文件,它就会为您完成所有工作——试试吧!



In answer to your suplementary question: An IBOutlet DOES NOT PARTICULARLY NEED TO BE a property - you can just use a normal cheap iVar. On the other hand, if you wish, to you can use a property. Furthermore: you can even use the new trick (2011) of not bothering to declare the ivar, just use the property declaration, and shove an IBOutlet in there!

回答您的补充问题:IBOutlet 并不特别需要成为财产 - 您可以只使用普通的廉价 iVar。另一方面,如果您愿意,可以使用属性。此外:您甚至可以使用新技巧 (2011),无需费心声明 ivar,只需使用属性声明,然后将 IBOutlet 放入其中!

回答by Rog

Another one that not many people are aware of is that you can drag from Interface Builder to your header file to create IBActions or IBOutlets.

另一个没有多少人知道的是,您可以从 Interface Builder 拖动到您的头文件以创建 IBActions 或 IBOutlets。

NOTE:Open the Assistant Editor with focus on the XIB inside IB.

注意:打开助手编辑器,重点放在 IB 内的 XIB。

If you create your IBOutlets this way Xcode automatically adds the property, synthesizes it, sets it to nil in viewDidUnload and releases it in dealloc.

如果你以这种方式创建你的 IBOutlets,Xcode 会自动添加属性,合成它,在 viewDidUnload 中将它设置为 nil 并在 dealloc 中释放它。

You simply drag from the IB object to your header file the same way you would when creating connections between the view objects and their file owners (screenshot below). enter image description here

您只需像在视图对象与其文件所有者之间创建连接(下面的屏幕截图)一样,从 IB 对象拖动到您的头文件。 在此处输入图片说明

回答by Matthias Bauch

There is an awesome tool that speeds the whole process up: accessorizer.

有一个很棒的工具可以加快整个过程:accessorizer

You write controlType varName;select it and press some buttons and accessorizer will create the property, init, dealloc, viewDidUnload and much more for you. You just have to paste the code into your project.

您编写controlType varName;选择它并按下一些按钮,访问器将为您创建属性、init、dealloc、viewDidUnload 等等。您只需将代码粘贴到您的项目中。

Try it, a demo is available.

试试吧,有演示可用。

回答by skorulis

You can save yourself having to release the object by changing the property declaration. If you use:

您可以通过更改属性声明来避免释放对象。如果您使用:

@property (assign) IBOutlet controlType varName;

retain wont be called on your view so you wont have to release it later. This is generally safe as views are retained when they are added to a parent. If you are removing the views from their parent for some reason then you will have to retain them.

不会在您的视图中调用保留,因此您以后不必释放它。这通常是安全的,因为视图在添加到父级时会被保留。如果您出于某种原因从其父级中删除视图,则必须保留它们。

回答by Kirby Todd

Here'san xcode script to automate the tedium of declaring properties.

这是一个 xcode 脚本,用于自动化声明属性的乏味。