当我可以使用内联变量时,为什么要声明@property?

编程入门 行业动态 更新时间:2024-10-26 01:31:13
本文介绍了当我可以使用内联变量时,为什么要声明@property?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我花了几天时间学习Objective-C并且有一些关于 @property 的问题。我有使用C#的经验,所以理解需要指针,初始化等。

举个例子:

@interface MyClass:NSObject { IBOutlet UIImageView * image; } @property(保留,非原子)UIImageView * image @end @implementation MyClass @synthesise image @end

我明白 @synthesise 用于创建 @property 。但是我有几个问题只是为了帮助我清理一切:

  • @property 复制或替换我的原始定义,还是只设置原始的多重性和原子性?
  • @synthesise 删除我需要使用 image = [[UIImageView alloc] init] ?
  • 如果我没有提供 @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 @end

    I understand that @synthesise is used to create the @property. But I have a few questions just to help me clear things up:

  • Does the @property duplicate or replace my original definition, or does it merely set up the mutibility and atomicity of the original?
  • Does @synthesise remove my need to use image = [[UIImageView alloc] init]?
  • If I do not provide a @property and still go ahead creating and destroying my variable manually, does that make any difference?
  • 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?

    本文发布于:2023-11-14 03:05:39,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1586044.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:内联   当我   可以使用   变量   声明

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!