tags:

views:

356

answers:

2

As someone that's new to Objective-C can someone give me an overview of the retain, assign, copy and any others I'm missing, that follow the @property directive? What are they doing and why would I want to use one over another?

Thanks

+1  A: 

http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocProperties.html, section 'attributes'. First result on Google for 'apple @property objective c'.

MrMage
Thanks for the link, I had read this but it didn't make a lot of sense to me.
Mark Reid
A: 

"retain" returns a retained reference to your object (caller's responsibility to release / dispose of it / it calls "retain" before returning a value, often combined with "autorelease"). "assign" (default) simply sets the pointer for situations that the caller & callee doesn't need to clean up (rarely w/o autorelease pools). You'd want a "retain" specifier whenever you're returning something that the caller understands that it owns the result... Things that your callee takes ownership over or are autoreleased. "copy" semantics are exactly like you'd expect. It's a byte-for-byte copy of the object (if it's written correctly).

Pestilence
Thanks for that, I'd read the Apple docs but it didn't make a lot of sense to me.
Mark Reid
Sorry, this answer is *wrong*. retain/assign/copy specify what the receiver does with values passed to it, not some wacky extra retaining before passing stuff out. Returned values should never be retained on the callers behalf. If the caller wants to retain a returned object, that's up to them.
walkytalky
@walkytalky wow. you're right. i don't even remember typing this. ugh. how drunk was i? sorry @Mark Reid. i'd totally unaccept this answer if I were you. it needs to be nuked / cleanup SO.
Pestilence