@property 与 ARC 的定义:强还是保留?

发布于 2024-12-10 12:31:53 字数 416 浏览 0 评论 0原文

使用 Xcode 4.2 和 ARC,我注意到 NSManagedObject 自动生成的代码对于属性来说仍然是这样的:

@property (nonatomic, retain) NSString * someString;

1) Shouldn't retain now be Replaced with 还是

2) 为什么自动生成的代码仍然使用 retain

3) 此属性语句中 retain 的正确替换是什么?

我目前正在使用 NSFetchRequest 调试问题,我认为这可能是问题的根源。想法?

Using Xcode 4.2 and ARC, I notice that the auto-generated code for an NSManagedObject still reads like this for properties:

@property (nonatomic, retain) NSString * someString;

1) Shouldn't retain now be replace with strong or weak?

2) Why does the auto-generated code still use retain

3) What is the correct replacement for retain in this property statement?

I'm currently debugging a problem using NSFetchRequest, and I thought this might be the source of the problem. Thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

青春有你 2024-12-17 12:31:53

1)retain现在不应该被替换为strong或weak吗?

不可以。你不能用weak代替retain;他们是不同的。强是 100% 保留的同义词;它们是相同的。您可以使用其中任何一个,因此这里没有“应该”。如果您愿意,可以将保留替换为强,但不是必须这样做。

2)为什么自动生成的代码仍然使用retain

为什么不呢?参见(1)。保留是正确的,所以没有问题。

3) 在此属性声明中,retain 的正确替换是什么?

无需更换retain。

我目前正在使用 NSFetchRequest 调试一个问题,我认为这可能是问题的根源。想法?

事实并非如此。

1) Shouldn't retain now be replace with strong or weak?

No. You cannot replace retain with weak; they are different. And strong is a 100% synonym for retain; they are identical. You can use either, so there is no "should" here. You can replace retain with strong if you like, but you don't have to.

2) Why does the auto-generated code still use retain

Why not? See (1). retain is correct so there is no problem.

3) What is the correct replacement for retain in this property statement?

There is no need to replace retain.

I'm currently debugging a problem using NSFetchRequest, and I thought this might be the source of the problem. Thoughts?

It isn't.

恍梦境° 2024-12-17 12:31:53

一并回答所有三个问题:retainstrong 是同义词,因此两者都是正确的。 文档指出

retain 暗示 __strong 所有权

strong 暗示 __strong 所有权

To answer all three questions in one: retain and strong are synonymous with each other, so both are correct. The documentation states

retain implies __strong ownership

strong implies __strong ownership

别在捏我脸啦 2024-12-17 12:31:53

在 ARC 之前,您必须“释放”保留的对象。这意味着保留有对应的部分。 ARC之后你不需要释放。所以用强。这是一个视觉线索,表明您不需要调用发布。

Before ARC, you have to 'release' an object which is retained. That mean retain has counter part. After ARC you don't need to release. So use strong. Its a visual clue that you don't need to call release.

鼻尖触碰 2024-12-17 12:31:53

“留”等于“强”。

例如使用“strong”:

@property (nonatomic, strong) NSString * someString;

并且例如使用“__strong”:

-(void) someMethod
{
    __strong NSString* vStr = [[NSString alloc] initWithString:@"some string"];
}

在 Apple Docs 上。说:

属性属性

引入关键字weak和strong作为新声明的属性属性,如以下示例所示。

// The following declaration is a synonym for: @property(retain) MyClass *myObject;
property(strong) MyClass *myObject;

苹果文档。 http://developer.apple.com/库/ios/#releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html

"retain" is equals to "strong".

"strong" is used for example:

@property (nonatomic, strong) NSString * someString;

And "__strong" is used for example:

-(void) someMethod
{
    __strong NSString* vStr = [[NSString alloc] initWithString:@"some string"];
}

On Apple Docs. says:

Property Attributes

The keywords weak and strong are introduced as new declared property attributes, as shown in the following examples.

// The following declaration is a synonym for: @property(retain) MyClass *myObject;
property(strong) MyClass *myObject;

Apple doc. http://developer.apple.com/library/ios/#releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文