ios 在函数内部初始化一个实例以供外部使用
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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将该行替换为这一行:
我在这里写了完整的解释: 属性的内存管理保留属性
Replace that line with this one:
I wrote full explanation here : Memory Management for properties with retain attribute
由于您的属性是用“retain”定义的,因此使用点符号将导致额外的保留。从 [[NSMutableArray alloc] init] 返回的保留计数为 1,然后当您使用属性声明生成的 setter 函数设置属性时,它的保留计数将为 2。
要修复,可以:
或者,你也可以这样做:
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:
Or, you could also do this:
有一个很好的方法来处理这个问题(并且您在创建 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.