I have read a lot of topics about getters and setters. I know what they are and why are useful. Different source claim's different ways to release the ivars. Here begins my confusion
#interface CoolClass : NSObject
{
NSString *name;
}
#property (nonatomic, copy) NSString *name;
#end
#implementation CoolClass
#synthesize name = _name;
-(id)init
{
if(super = [self super])
{
self.name = #"Jo";
}
return self;
}
-(void)dealloc
{
[self.name release], self.name = nil;
}
#end
Is that the correct way to release/free ivars ?
You'll want to use accessors most of the time, but not in partially constructed states because they can have negative side-effects. Here's how it's done:
- (id)init
{
if((self = [super init])) {
// self.name = #"Jo"; << don't use accessors in initializer
_name = [#"Jo" copy]; << good
}
return self;
}
// added for another variation:
- (id)initWithName:(NSString *)inName
{
if((self = [super init])) {
_name = [inName copy];
}
return self;
}
- (void)dealloc
{
// don't use accessors in dealloc
// don't release the result of a getter (release the result of the retained or copied result)
// [self.name release], self.name = nil;
// instead:
[_name release], _name = nil;
[super dealloc]; << compiler should have warned you about this one
}
Note: In the case of init, the string literal is an immortal and it won't matter if you copy it because the copy just returns itself. My preference is to just 'copy' the immortal for clarity, although it's unnecessary.
Here is what I would advise:
#interface CoolClass : NSObject
#property (nonatomic, copy) NSString *name;
#end
#implementation CoolClass
#synthesize name = _name;
-(id)init
{
if(super = [self super])
{
self.name = #"Jo";
}
return self;
}
-(void)dealloc
{
[_name release];
[super dealloc];
}
#end
Notes:
There is no need to explicitly declare ivars inside { ... } in your header. They will be created automatically when you synthesise your property. Explicit ivars are a legacy concept that are no longer needed since about iOS 3.
You should not use self.name in the dealloc as this calls the getter method, which may do additional work beyond merely fetching the ivar. Normally it's good practice to use the getter method, but in the dealloc you should release the ivar directly
It is good practice to set ivars to nil after releasing them, but again in the dealloc this in not necessary because no code is ever executed after dealloc, so the pointer won't be referenced again.
Normally (outside of the dealloc), if you wish to release an ivar you should set it to nil using the setter like this: self.name = nil; that will automatically release it and set it to nil. This is equivalent to [_name release], _name = nil;
#interface CoolClass : NSObject
{
NSString *name;
}
You declared here an instance variable 'name'; Nowadays there is no need to declare ivars in the header file. Just use properties and make the compiler to synthesize ivar for you.
#property (nonatomic, copy) NSString *name;
Here we have a property declaration that specifies that a copy of the object should be used for assignment and that a previous value is sent a release message.
In implementation you want to synthesize your property:
#synthesize name = _name;
This code tells the compiler to generate a getter and setter for property called 'name' and use instance variable called '_name' to store value. So you have now two ivars - 'name' and '_name'.
That how init method should like like:
-(id)init
{
if(self = [super init])
{
name = #"This is ivar declared between {}";
_name = #"synthesized ivar";
}
return self;
}
And the dealloc:
-(void)dealloc
{
[name release];
[_name release];
[super dealloc];
}
Related
My understanding is that instance variables should be accessed directly from inside the init method. For example:
#interface ABC : NSObject
#property (strong, nonatomic) NSString *name;
#end
#implementation ABC
- (id)init
{
if ((self = [super init]) != nil)
{
_name = #"some name";
}
}
// another init example
- (id)initWithName:(NSString*)n
{
if ((self = [super init]) != nil)
{
_name = n;
}
}
#end
I am wondering about the _name variable. In both init examples, is _name retained? For this example, I am using ARC.
Whether _name is retained in this code depends on whether you have ARC turned on. If you do, ARC will retain the object for you (since that is ARC's job). If you don't have ARC turned on, you need to retain it yourself, which would look like:
- (id)initWithName:(NSString*)n
{
if ((self = [super init]) != nil)
{
_name = [n retain];
}
}
(It's also worth pointing out that NSStrings should usually be copied rather than retained, so you would make the property #property (copy, nonatomic) NSString *name; and the assignment would be _name = [n copy].)
How exactly getter and setter methods body looks like after they have been automatically synthesized ?
From official documentation I found so far only recommended implementation techniques, however no word about which of them used by compiler during synthesizing process: http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmAccessorMethods.html#//apple_ref/doc/uid/TP40003539-SW5
Some techniques recommends implementations containing autorelease message, which is not quite secure for multithreaded programming. I'm just wonder if auto-generated code follows to some of the proposed implementations.
For example:
.h
#interface AClass: NSObject{}
#property (nonatomic, retain) AnotherClass *aProp;
#end
.m
#implementation AClass
#synthesize aProp
-(id) init {
if ((self = [super init])) {
self.aProp = [[AnotherClass alloc] init]; // setter invocation
}
return self;
}
-(AnotherClass *) aMethod {
return self.aProp; // getter invocation
}
#end
What are equivalent accessors code snippets for aProp generated by compiler ?
-(AnotherClass *) aProp {
// getter body
}
-(void) setAProp: (AnotherClass *) {
// setter body
}
When declaring a property as nonatomic, you'll get the following:
// .h
#property (nonatomic, retain) id ivar;
// .m
- (id)ivar {
return ivar;
}
- (void)setIvar:(id)newValue {
if (ivar != newValue) { // this check is mandatory
[ivar release];
ivar = [newValue retain];
}
}
Note the check ivar != newValue. If it was absent, ivar could be dealloc'ed after release, and the following retain would cause a memory access error.
When you declare your property with copy, the code will look almost the same, with retain replaced by copy.
For assign, it is even simpler:
- (void)setIvar:(id)newValue {
ivar = newValue;
}
Now, when you declare your property as atomic (this one is the default), things get slightly more complicated. A snippet similar to the one below was posted by one of Apple's engineers on the development forums:
- (id)ivar {
#synchronized (self) {
return [[self->ivar retain] autorelease];
}
}
- (void)setIvar:(id)newValue {
#synchronized (self) {
if (newValue != self->ivar) {
[self->ivar release];
self->ivar = newValue;
[self->ivar retain];
}
}
}
Note the #synchronized block in both methods and additional retain-autorelease in the getter. Both those things ensure that you will either get the previous value (retained and autoreleased) or a new one in the case the value is changed by some thread while you are trying to read it.
It depends on the attributes you set, for instance with a RETAIN attribute...
- (void)setAProp:(AnotherClass *)value {
[aProp release];
aProp = value;
[aProp retain];
}
For ASSIGN attribute....
- (void)setAProp:(AnotherClass *)value {
aProp = value;
}
For COPY attribute (used with NSString)...
- (void)setAProp:(AnotherClass *)value {
aProp = [value copy];
}
I created a simple UIViewController with a custom init method like this:
-(id)initWithGroupNumber:(NSString *)groupNumber {
if ((self = [super init]) == nil) {
return nil;
}
self.levelGroup = groupNumber;
return self; }
levelGroup is a property written in the .h file
#property (nonatomic, retain) NSString *levelGroup;
When I call the method above this way:
LevelsViewController *lvc = [[LevelsViewController alloc]initWithGroupNumber:#"5"];
the controller is allocated but all the property inside are set to nil. I can't understand why.
First of all when you deal with classes that have subclass of type mutable (e.g. NSMutableString), use copy.
So, your property should become:
#property (nonatomic, copy) NSString *levelGroup;
Then, inside the UIViewController synthesize the property
#synthesize levelGroup;
and in init do the following:
-(id)initWithGroupNumber:(NSString *)groupNumber {
if (self = [super init]) {
levelGroup = [groupNumber copy];
}
return self;
}
As written in the memory management guide you should not use self. inside init and in dealloc.
Use your property self.levelGroup to get or set the value.
Remember to release in dealloc:
[levelGroup release];
Hope it helps.
Replace your
self.levelGroup = [[NSString alloc]init];
with
self.levelGroup = groupNumber; // actually uses your init value.
How exactly getter and setter methods body looks like after they have been automatically synthesized ?
From official documentation I found so far only recommended implementation techniques, however no word about which of them used by compiler during synthesizing process: http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmAccessorMethods.html#//apple_ref/doc/uid/TP40003539-SW5
Some techniques recommends implementations containing autorelease message, which is not quite secure for multithreaded programming. I'm just wonder if auto-generated code follows to some of the proposed implementations.
For example:
.h
#interface AClass: NSObject{}
#property (nonatomic, retain) AnotherClass *aProp;
#end
.m
#implementation AClass
#synthesize aProp
-(id) init {
if ((self = [super init])) {
self.aProp = [[AnotherClass alloc] init]; // setter invocation
}
return self;
}
-(AnotherClass *) aMethod {
return self.aProp; // getter invocation
}
#end
What are equivalent accessors code snippets for aProp generated by compiler ?
-(AnotherClass *) aProp {
// getter body
}
-(void) setAProp: (AnotherClass *) {
// setter body
}
When declaring a property as nonatomic, you'll get the following:
// .h
#property (nonatomic, retain) id ivar;
// .m
- (id)ivar {
return ivar;
}
- (void)setIvar:(id)newValue {
if (ivar != newValue) { // this check is mandatory
[ivar release];
ivar = [newValue retain];
}
}
Note the check ivar != newValue. If it was absent, ivar could be dealloc'ed after release, and the following retain would cause a memory access error.
When you declare your property with copy, the code will look almost the same, with retain replaced by copy.
For assign, it is even simpler:
- (void)setIvar:(id)newValue {
ivar = newValue;
}
Now, when you declare your property as atomic (this one is the default), things get slightly more complicated. A snippet similar to the one below was posted by one of Apple's engineers on the development forums:
- (id)ivar {
#synchronized (self) {
return [[self->ivar retain] autorelease];
}
}
- (void)setIvar:(id)newValue {
#synchronized (self) {
if (newValue != self->ivar) {
[self->ivar release];
self->ivar = newValue;
[self->ivar retain];
}
}
}
Note the #synchronized block in both methods and additional retain-autorelease in the getter. Both those things ensure that you will either get the previous value (retained and autoreleased) or a new one in the case the value is changed by some thread while you are trying to read it.
It depends on the attributes you set, for instance with a RETAIN attribute...
- (void)setAProp:(AnotherClass *)value {
[aProp release];
aProp = value;
[aProp retain];
}
For ASSIGN attribute....
- (void)setAProp:(AnotherClass *)value {
aProp = value;
}
For COPY attribute (used with NSString)...
- (void)setAProp:(AnotherClass *)value {
aProp = [value copy];
}
Is it safe to say that if a class member does not need getter or setter functions then there's no point in making them properties and synthesizing them?
Well, yes, but often properties can be helpful in the implementation itself even if the properties won't be set outside of the implementation.
For example, suppose you had
#interface SomeObject : NSObject {
NSThing *thing;
}
#end
#implementation SomeObject
- (id)init {
if((self = [super init]))
thing = [[NSThing someThing] retain];
return self;
}
- (void)someMethod {
if(thing)
[thing release];
thing = [[NSThing someOtherThing] retain];
}
// etc etc
#end
Why would you want to bother having to check if thing had been allocated, release thing, set it to something else, and then retain it again, when you could simply do:
- (id)init {
if((self = [super init]))
[self setThing:[NSThing someThing]];
return self;
}
- (void)someMethod {
[self setThing:[NSThing someOtherThing]];
}
If you don't want to make these properties accessible outside of your class, you can use a category
#interface SomeObject ()
#property (retain) NSThing *thing;
#end
in your .m file.