Application crashes if a property name starts with new

廉价感情. 提交于 2019-11-29 15:22:10

问题


In my project I'm using coredata. One of the entity has an attribute named newTotal, in its corresponding NSManagedObject class the property declaration is like

@property (nonatomic, strong) NSString *newTotal;

If I add a property like this in an NSObject subclass the XCode will show an error like

error: property's synthesized getter follows Cocoa naming convention for returning 'owned' objects

But in NSManaged object subclasses it's not showing the error but the application crashes when the property is accessed, something saying like EXC_BAD_ACCESS.

Why XCode not showing error but application crashes?. Is this a bug with XCode/clang/LLVM ?

I know its something related to the synthesize. NSManagedObject sub classes not synthesizing the property in it, but the @dynamic directive just tells the compiler that the getter and setter methods are implemented not by the class itself but somewhere else (like the superclass or will be provided at runtime). But I have no clear Idea about this. Can anybody can give a clear idea about the problem?


回答1:


I can see you are using ARC.

In ARC memory is managed for you, but there are few things you can/have to do yourself. You cannot name property "newXxxx" because:

https://developer.apple.com/library/mac/releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html

You cannot give an accessor a name that begins with new. This in turn means that you can’t, for example, declare a property whose name begins with new unless you specify a different getter:

// Won't work:

@property NSString *newTitle;

// Works:

@property (getter=theNewTitle) NSString *newTitle;


来源:https://stackoverflow.com/questions/21304385/application-crashes-if-a-property-name-starts-with-new

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!