我花了几天时间学习Objective-C并且有一些关于 @property 的问题。我有使用C#的经验,所以理解需要指针,初始化等。
举个例子:
@interface MyClass:NSObject { IBOutlet UIImageView * image; } @property(保留,非原子)UIImageView * image @end @implementation MyClass @synthesise image @end我明白 @synthesise 用于创建 @property 。但是我有几个问题只是为了帮助我清理一切:
最终,是2之间的区别,@ property在内存管理和多线程方面为您提供了更大的灵活性,而正常的方法为您提供了默认值。
解决方案
@prototype是复制还是替换我的原始定义,还是只设置原始的多重性和原子性?
图像的ivar声明是多余的 。
前者声明了一个ivar(类型+名称+实例存储)。 / p>
属性声明指定类型,名称,存储(在最近的编译器版本中),声明访问器方法(例如 - (UIImageView *)image; 和 - (void)setImage:(UIImageView *)pImage; )等属性说明符(在编译器生成访问器时使用)。
@synthesise是否删除了我使用image = [UIImageView的需要]分配]?
否。您仍然需要适当地实现初始化程序和 dealloc (在MRC中)。
如果我不提供@property并且仍然继续手动创建和销毁我的变量,那会有什么不同吗?
那会当你不想/需要为你生成样板访问器方法时,没关系。这是一个设计选择。并不是每个ivar都需要访问器方法。
最终,2,@ property之间的差异为内存管理提供了更大的灵活性多线程和普通的线程为您提供默认值。
它们存在的最大原因是方便性。属性保存了很多样板代码。
属性没有更多灵活性 - 属性实现了最实际的用途。
很少见,原子性(在此上下文中)等同于正确的线程安全性和正确的并发执行。
I have spent a few days learning Objective-C and have a few questions about @property. I have experience with C# so understand the need for pointers, initialization etc.
So as an example:
@interface MyClass : NSObject { IBOutlet UIImageView *image; } @property (retain, nonatomic) UIImageView *image @end @implementation MyClass @synthesise image @endI understand that @synthesise is used to create the @property. But I have a few questions just to help me clear things up:
Ultimately, is the difference between the 2, @property gives you more flexibility with regards to memory management and multi-threading and the normal one gives you the defaults.
解决方案Does the @prototype duplicate or replace my original definition, or does it merely set up the mutibility and atomicity of the original?
The ivar declaration of image is redundant when using the most recent compiler releases.
The former declares an ivar (type + name + instance storage).
The property declaration specifies the type, name, storage (in more recent compiler releases), declares the accessor methods (e.g. - (UIImageView *)image; and - (void)setImage:(UIImageView *)pImage;), and other property specifiers (which are used when the accessors are generated by the compiler).
Does @synthesise remove my need to use image = [UIImageView alloc]?
No. You still need to implement your initializer and dealloc (in MRC) appropriately.
If I do not provide an @property and still go ahead creating and destroying my variable manually, does that make any difference?
That would be fine, when you do not want/need boilerplate accessor methods generated for you. It's a design choice. Not every ivar needs accessor methods.
Ultimately, is the difference between the 2, @property gives you more flexibility with regards to memory management and multi-threading and the normal one gives you the defaults.
The biggest reason they exist is convenience. Properties save a lot of boilerplate code.
There is no more flexibility with properties -- properties implement the most practical uses.
It's infrequent that atomicity (in this context) is equivalent to proper thread safety and correct concurrent execution.
更多推荐
当我可以使用内联变量时,为什么要声明@property?
发布评论