Question about synthesized properties.

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

  1. macrumors 6502

    Joined:
    Jan 3, 2009
    #1
    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 :) )

    Code:
    /*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.
     
  2. macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #2
    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.
     
  3. thread starter macrumors 6502

    Joined:
    Jan 3, 2009
    #3
    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.
     
  4. macrumors member

    Joined:
    Oct 9, 2008
    #4
    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. thread starter macrumors 6502

    Joined:
    Jan 3, 2009
    #5

    Thank you Peter
     

Share This Page