ios 在函数内部初始化一个实例以供外部使用

发布于 2024-12-04 08:23:28 字数 310 浏览 5 评论 0原文

xcode 分析器告诉我,一个方法返回一个具有 +1 保留计数的 Objective-C 对象: 在此处输入图像描述

但 self.athletes 是我在功能之外也需要的对象......我该如何解决这个‘警告? 再次感谢

运动员声明如下:

NSMutableArray *athletes;
@property (nonatomic, retain) IBOutlet NSMutableArray *athletes;

the xcode analyzer tell me that a method returns an Objective-C object with a +1 retain count:
enter image description here

but the self.athletes is an object that I need also outside my function... how can I solve this 'warning?
thanks again

the athletes is declared like this:

NSMutableArray *athletes;
@property (nonatomic, retain) IBOutlet NSMutableArray *athletes;

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

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

发布评论

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

评论(3

泼猴你往哪里跑 2024-12-11 08:23:28

将该行替换为这一行:

self.athletes = [NSMutableArray array];

我在这里写了完整的解释: 属性的内存管理保留属性

Replace that line with this one:

self.athletes = [NSMutableArray array];

I wrote full explanation here : Memory Management for properties with retain attribute

|煩躁 2024-12-11 08:23:28

由于您的属性是用“retain”定义的,因此使用点符号将导致额外的保留。从 [[NSMutableArray alloc] init] 返回的保留计数为 1,然后当您使用属性声明生成的 setter 函数设置属性时,它的保留计数将为 2。

要修复,可以:

self.athletes = [NSMutableArray array]; // Returns an autoreleased object

或者,你也可以这样做:

athletes = [[NSMutableArray alloc] init]; // Doesn't use the setter generated by the property declaration, so doesn't retain again.

Since your property is defined with "retain", using the dot notation will result in an extra retain. The return from the [[NSMutableArray alloc] init] has a retain count of 1, and then when you set the property using the setter function generated by the property declaration it will have a retain count of 2.

To fix, either:

self.athletes = [NSMutableArray array]; // Returns an autoreleased object

Or, you could also do this:

athletes = [[NSMutableArray alloc] init]; // Doesn't use the setter generated by the property declaration, so doesn't retain again.
π浅易 2024-12-11 08:23:28

有一个很好的方法来处理这个问题(并且您在创建 UI 时已经使用了此模式)。

NSMutableArray *athletesTemp = [[NSMutableArray alloc] init];

self.athletes = 运动员Temp;

[运动员临时发布];

这里你不需要承担自动释放对象的负载。

There is a nice way to handle this (and you have already used this pattern while creating UI ).

NSMutableArray *athletesTemp = [[NSMutableArray alloc] init];

self.athletes = athletesTemp;

[athletesTemp release];

Here you don't need to carry the load of an auto release object.

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