PDA

View Full Version : Question about synthesized properties.




mdeh
Feb 25, 2009, 05:57 PM
I wonder if someone can clarify some things for me, which suddenly seem unclear.

Firstly, a snippet of some code. ( yes..it's from Kochan :) )


/*interface*/

@interface AddressBook : NSObject {

NSString *bookName;
NSMutableArray *book;

}

@property (copy, nonatomic) NSString * bookName;



/*implementation*/
@synthesize bookName;

-(AddressBook*) initWithName: (NSString *) nameOfBook
{
self = [super init];

if ( self)
{
self.bookName = nameOfBook;
book = [NSMutableArray arrayWithCapacity: 20];
return self;

}



1) Why does the compiler complain if one omits the attributes of NSString * property , but not if one omits it if , for example, it is a simple integer, say "int i" as an ivar? (If I omit the attributes , I get a warning that Xcode is applying the default ( retain) attribute, and then a second warning saying that default is inappropriate for a non-gc(garbage colletion) property name). Sorry if this sounds dumb, but it usually means I am missing something quite fundmental here.


2) Specifically about the line "self.bookName = nameOfBook;". My aim is **trying** to use the synthesized accessor method. What would happen if I omitted the "self" as in "bookName = nameOfBook". (Self, in the past has been redundant...but what I want to avoid is obviously doing a direct assign and bypassing using the synthesized accessors). Will the synthesized accessor method by default initially free the objects memory before creating the new object, or is this one of the directives one supplies with the attributes.

Any other dumb thing that I am missing? :)

Thank you as usual in advance.



Catfish_Man
Feb 25, 2009, 06:10 PM
int is not an Objective-C object, so does not require (or support) retaining and releasing. If you omit the self. then it won't use the accessor, and won't retain/release things correctly.

See http://www.stepwise.com/Articles/Technical/2001-03-11.01.html or similar for more info on memory management. It doesn't mention synthesized properties, but really all they're doing is writing the usual accessor idiom for you.

mdeh
Feb 25, 2009, 06:39 PM
See http://www.stepwise.com/Articles/Technical/2001-03-11.01.html or similar for more info on memory management. It doesn't mention synthesized properties, but really all they're doing is writing the usual accessor idiom for you.

Thanks...great article. I was going to ask about setter semantics, but, low and behold, there is documentation about setter semantics!!!

So, from the article you quoted, and from the "semantics" article, the warning makes sense? If one were to simply assign the new object ( the default attribute) (without releasing, as the attribute copy does), it would lead to memory leak? Am I understanding this correctly?

thanks.

Peter Maurer
Feb 28, 2009, 11:35 AM
If one were to simply assign the new object ( the default attribute) (without releasing, as the attribute copy does), it would lead to memory leak?

It wouldn't get retained in the first place. So you'd probably have to retain it yourself, and in that case, yes, you'd have to release it eventually.

(I'm leaving singletons and garbage collection out of this for now.)

mdeh
Feb 28, 2009, 07:21 PM
It wouldn't get retained in the first place. So you'd probably have to retain it yourself, and in that case, yes, you'd have to release it eventually.

(I'm leaving singletons and garbage collection out of this for now.)


Thank you Peter