Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mdeh

macrumors 6502
Original poster
Jan 3, 2009
345
2
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.
 

Catfish_Man

macrumors 68030
Sep 13, 2001
2,579
2
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 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

macrumors 6502
Original poster
Jan 3, 2009
345
2
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

macrumors member
Oct 9, 2008
71
25
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

macrumors 6502
Original poster
Jan 3, 2009
345
2
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.