以“new”开头的属性名称在iOS中导致BAD_ACCESS错误 [英] Property name starting with 'new' prefix leads to BAD_ACCESS error in iOS

查看:495
本文介绍了以“new”开头的属性名称在iOS中导致BAD_ACCESS错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的属性被声明在我的NSManagedObject类名称为newPrice,它导致僵尸对象。经过几个小时的调试,我发现有一个问题,方法是释放这个对象,但不保留它。将此属性重命名为priceNew后,一切都很顺利。我不明白为什么会造成问题。



财产声明:

  @property(nonatomic,retain)NSNumber * newPrice; 

此调用导致问题:

  [self setPieceStateWithPrice:self.action.newPrice]; 

传递重命名的参数,如self.action.priceNew一切顺利...

$



在Objective-C命名约定中,方法的以 new 开头的名称将返回一个保留对象。使用ARC,该命名约定成为一个要求。这意味着一个正常的,ARC编译的方法不应该以 new 开头,因为编译器会假定它的保留计数为1.



引用文档:


您创建的任何对象



您使用名称以alloc开头的方法创建一个对象,
new,copy或mutableCopy(例如alloc,newObject或
mutableCopy)。



My property was declared in my NSManagedObject class with name "newPrice", which leads to "zombie object". After some hours of debugging I figured out that there is problem with method which is releasing this object but not retaining it. After renaming this property to "priceNew" everything goes well. I don't understand why this is causing problem.

Declaration of property:

@property (nonatomic, retain) NSNumber * newPrice;

This call causing problem:

[self setPieceStateWithPrice:self.action.newPrice];

After passing renamed argument like self.action.priceNew everything goes well...

解决方案

Don't do that.

In Objective-C naming conventions, methods whose names begin with new are expected to return a retained object. With ARC, that naming convention becomes a requirement. That means that a normal, ARC-compiled method should never start with the name new because the compiler will assume that it already has a retain count of 1.

To quote the docs:

You own any object you create

You create an object using a method whose name begins with "alloc", "new", "copy", or "mutableCopy" (for example, alloc, newObject, or mutableCopy).

这篇关于以“new”开头的属性名称在iOS中导致BAD_ACCESS错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆