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.
Related
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.
I'm playing around with Julius Guzy's sample code for creating a document based application that can save text files. I ran his code and it works perfectly and the app runs/save files (All in the latest version of Xcode. Though Guzy's sample code compiles using the 10.6 SDK). I'm trying to learn from it and rewrite the code. I have everything in order to compile the app but for the strangest thing in MyDocument.m. I am unable to run it.
#synthesize nsTextViewObj;
Xcode is giving me this error, which I don't really understand it.
Existing ivar 'nsTextViewObj' for property 'nsTextViewObj' with
assign attribute must be __unsafe_unretained
nsTextViewObj; is suppose to be linked to the "File Owners" in IB. I did that in the first place. It beats me why I'm getting that error. SDK conflicts maybe? How can I fix this?
ARC requires some changes to the code. Xcode provides a tool to automatically convert non-ARC code to ARC: Edit / Refactor / Convert to Objective-C ARC…
The Xcode tool is a good start, still I would recommend you to read Transitioning to ARC Release Notes.
I don't know Julius Guzy's code, but probably you should change it to something like:
#interface ifc_name : NSWindowController {
NSTextView *__unsafe_unretained nsTextViewObject;
}
#property (unsafe_unretained) NSTextView *nsTextViewObject;
As the name implies, unsafe_unretained is not safe: when the object is deallocated, your reference is invalid. If you are deploying only to iOS 5 or higher, and OS-X 10.7 or higher, you can use weak instead. With weak your reference will become zero when the object is deallocated.
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.
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
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).