Question about synthesized properties.

Discussion in 'Mac Programming' started by mdeh, Feb 25, 2009.

  1. mdeh macrumors 6502

    Jan 3, 2009
    I wonder if someone can clarify some things for me, which suddenly seem unclear.

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

    @interface AddressBook : NSObject {
    	NSString *bookName;
    	NSMutableArray  *book;
    @property (copy, nonatomic) NSString * bookName;
    @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.
  2. Catfish_Man macrumors 68030


    Sep 13, 2001
    Portland, OR
    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 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.
  3. mdeh thread starter macrumors 6502

    Jan 3, 2009
    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?

  4. Peter Maurer macrumors member

    Oct 9, 2008
    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.)
  5. mdeh thread starter macrumors 6502

    Jan 3, 2009

    Thank you Peter

Share This Page