Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode warning if using "assign" attribute for reference types (objects)

Is there an Xcode warning, or any kind of way to get a warning, when one declares an object (reference type) property using the assign attribute in ARC:

@property (nonatomic, assign) NSNumber *myNumber;

I converted a long property to NSNumber and accidentally forgot to change the attribute from assign to strong.

There are no compile-time warnings or errors, and the run-time error that one would get would only happen sometimes and it is a very obscure crash. Only while debugging is when one would get a crash and an error like "message sent to a deallocated instance" and that's because of the use of zombie objects in development.

For non-debug builds, the crash doesn't happen often and it is reported (by Crashlytics, for example) as EXC_BAD_ACCESS - KERN_INVALID_ADDRESS. I'm assuming that the crash is caused by this issue.

I understand that assign is a valid option for an object if you want to maintain a weak reference to it, and you don't want the pointer to automatically become NULL when the object gets deallocated. However, I imagine there should be a warning one can turn on or off because assign is not something you normally want to use in ARC, but I can't find it in the build settings.

like image 752
Roberto Avatar asked Aug 07 '15 14:08

Roberto


2 Answers

Unfortunately, I couldn't find any warning flag for that. It would be a cool thing to have though. You can refer to this site for a list of all clang warnings you can enable in Xcode.

like image 134
utogaria Avatar answered Oct 03 '22 21:10

utogaria


This is not technically an answer to my question because my question asks whether there is a setting to enable warning and the answer to that is "no" as per the accepted answer.

However, if you want to find such cases, you can do a global search using regular expression. You have to switch the find options to "Regular Expression".

To find something like

@property (nonatomic, assign) NSNumber* ...

you can use the regex

assign\) [a-zA-Z]+\*

You can play around with it. For example, if you put a space between the type and the asterisk in your property declaration like this

@property (nonatomic, assign) NSNumber * ...

you just need to add a space before the slash that escapes the asterisk like this

assign\) [a-zA-Z]+ \*

like image 22
Roberto Avatar answered Oct 03 '22 20:10

Roberto