ios 什么是强属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6701023/
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-08-30 20:46:26  来源:igfitidea点击:

What is the strong property attribute

iphoneobjective-cioscocoamacos

提问by Chance Hudson

I am using the Xcode beta for developers, and am noticing some subtle differences. Among them is a new attribute for declared properties.

我正在为开发人员使用 Xcode 测试版,并注意到一些细微的差异。其中有一个用于声明属性的新属性。

@property(strong)IBOutlet NSArrayController *arrayControl;

My question is: what does the strong attribute mean?? Does it replace some older one, or is it something entirely new? I have searched through google and the developer documentation and havent been able to find anything. Until i know what it is i am hesitant to use it.

我的问题是:strong 属性是什么意思?它会取代一些旧的,还是全新的?我已经搜索过谷歌和开发人员文档,但没有找到任何东西。在我知道它是什么之前,我对使用它犹豫不决。

Thanks in advance

提前致谢

回答by Lily Ballard

It's a replacement for the retainattribute, as part of Objective-C Automated Reference Counting (ARC). In non-ARC code it's just a synonym for retain.

它是retain属性的替代品,作为Objective-C 自动引用计数 (ARC) 的一部分。在非 ARC 代码中,它只是retain.

回答by Hyman Farnandish

A strong reference is a reference to an object that stops it from being deallocated. In other words it creates a owner relationship. Whereas previously you would do this:

强引用是对阻止它被释放的对象的引用。换句话说,它创建了所有者关系。而以前你会这样做:

**// Non-ARC Compliant Declaration
@property(retain) NSObject *obj;**

Under ARC we do the following to ensure a class instance takes an ownership interest a referenced object (i.e. so it cannot be deallocated until the owner is).

在 ARC 下,我们执行以下操作以确保类实例获得引用对象的所有权权益(即,在所有者存在之前不能释放它)。

**// ARC Compliant Declaration
@property(strong) NSObject *obj;**

回答by sanjeev sharma

As we know, we cannot release any object in an ARC-based project in iOS 5. So when we want to retain any object for further use at a later stage and don't want ARC to remove the object from memory, then we set the property for the object as "Strong".

我们知道,在 iOS 5 中,我们不能在基于 ARC 的项目中释放任何对象。 所以当我们想要保留任何对象以供以后进一步使用并且不希望 ARC 从内存中删除对象时,那么我们设置对象的属性为“强”。