Automatically created NSManagedObject subclasses don't use ARC - objective-c

My project is ARC enabled (the build settings have Objective-C Reference Counting set to YES). There are no file exceptions to this, it is enabled project wide. (Latest stable version of Xcode).
When I create an NSManagedObject subclass via File > New for a Core Data entity, the generated header uses the following in its property declarations:
#property (nonatomic, retain)
But 'retain' is not ARC!! Is this a bug, or is there something I'm missing or not understanding? There are no build warnings - if this is a bug though, how can I remedy it?

retain is a synonym for strong. So it is perfectly valid to use retain or strong in this case.
For more detail check out this question.

retain is a valid property declaration in ARC: See the clang documentation on it

Related

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.
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.
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]+ \*

Convert ARC code to non-ARC

I'm struggling to get an existing ARC-enabled control to run under Xcode 4.2 (OSX Snow Leopard), in a non-ARC-enabled project, and I'm facing various issues :
How should I fix the following issues ?
Use of (strong) in properties
Use of (nonatomic) in properties
Instance methods not found (not having being declared in the interface)
Enable new-style Objective-C
Also, it seems to be complaining about NSScrollerKnobStyle not being defined. Is it a 10.8-to-10.6 SDK-specific issue?
P.S. The control I'm using is ITSidebar
You're going to have to change those strong properties to retain or copy, as appropriate. There's nothing wrong with nonatomic in non-ARC code.
You may have to add #synthesize directives for your properties to get the compiler to add accessor methods. #synthesize is the default in the latest compiler.
There ae a number of other changes to the language, such as object literals. They're all well documented; you just need to apply them in reverse.
I'm not sure about NSScrollerKnobStyle, but if you look it up the documentation will tell you when it was introduced.

PhoneGap 2.0.1 Dealloc

I setup a phonegap 2.0.1 project and noticed that it is using ARC. However im wondering if the MainViewController.m which is automatically created for you, that inherits CDVViewController is also run under ARC.
I have added a few properties to the MainViewController like:
#property (nonatomic, readwrite, copy) NSString* errorJS;
And i am wondering If I need to use dealloc to release it, or weather this is already handeld as CDVViewController is run under ARC :S
Can anyone tell me if I still need to release my properties in MainViewController or not?
ARC basically means that you don't have to add release calls because the compiler adds them for you - the binary output from an ARC enabled project (eg libcordova.a) isn't any different from one where the release calls were added manually.
The answer depends on whether or not your project is set to use ARC. I believe the template has ARC turned off by default.
If ARC is enabled, calling release will produce a compiler error. If ARC is disabled, not calling release will usually produce a warning when you run static analysis.

How to use the "weak" in ARC?

I use:
#property(nonatomic, weak) IBOutlet UIView *videoView;
there is a warning:
Property 'videoView' requires method 'videoView' to be defined - use #synthesize, #dynamic or provide a method implementation in this class implementation
Then I try:
#synthesize videoView;
there is an error:
The current deployment target does not support automated __weak references.
And another question:
#property(nonatomic, unsafe_unretained) IBOutlet UIView *videoView;
- (void)dealloc {
videoView = nil;
}
Can I use this way?
The current deployment target does not support automated __weak references.
The issue is that iOS 4.x doesn't support auto-zeroing weak references. This means that, when a weakly-referenced object is destroyed, the weak references continue to point to it and may cause crashes if used.
Auto-zeroing weak references are supported in iOS 5 and newer. To take advantage of them and clear the warning above, raise your minimum iOS target to 5.0, and use the 5.0 SDK.
What is your deployment target? you need to have at least iOS4 to have weak references, and you need to be using LLVM4 with Xcode4.4 or later to be able to just declare your #property variables and not have to provide an #synthesize.
As for the second question - what is it that you are trying to do. If you are just trying to safely set the variable to nil on dealloc, then it is okay, since you are declaring it as unsafe_unretained you don't own it so you shouldn't release it.

didRecieveMemoryWarning in ARC (iOS / Obj-C)

I have an iOS (Obj-C) project that uses ARC (Automatic Reference Counting). According to Apple Documentation, and pure experimentation, you cannot have statements such as:
[UIThingamabob release];
This was previously the way to release items after being allocated or retained. Now I do know that you have to manage how IB objects are created in the
#property (nonatomic, retain) IBOutlet ...
portion of your header file.
I've been using the above statement as it is (with the nonatomic and retain (or strong- what's the difference anyway, how are they used?) properties) for all of my IB items. When I test on an iOS Device, I'll randomly get a UIAlertView (that I created for debugging purposes) that only displays when the didRecieveMemoryWarning event is fired.
Some of my BETA testers are bombarded with these views nonstop until they manage to quit the app.
My question is, what do I put in the didRecieveMemoryWarning event since I can't release objects? If there isn't anything to put there, then are these errors occurring due to the way I create my objects with the #property function?
You should use #property (nonatomic, weak) IBOutlet... for all of your IBOutlets. If you use strong, the outlet is retained by the view controller and by it's superview. When the view disappears, the view controller still has a reference to that outlet which is no longer visible. You could set the outlet property to nil in -viewDidUnload or by using weak setting the pointer to nil is done automatically when the view disappears.
You should not use retain in the #property statement. The ARC options are assign, weak and strong. For more details, see here:
https://developer.apple.com/library/mac/#releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html
In particular, check the part on Property Attributes!
edit: Thanks for the comments, both retain and strong seem to be equivalent right now.
It is impossible to know what to do without seeing your project but you can do things like:
Clear out arrays of data that you can regenerate later. Clear buffers. Set currently unused objects to nil (which will release them if they are not retained elsewhere).