Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: [OT] Retain count riddle



At 15:31 Uhr -0500 30.03.2004, Jonathan E. Jackel wrote:
This should sorta work:

#define SAFE_SET_PATH(old, new) NSString *temp = [[new lastPathComponent]
retain]; [old release]; old = temp;

Two suggestions:

a) When you create a macro that contains several statements, use curly brackets to give it its own scope. this avoids namespace collisions and oddities when using the Macro in a one-line"if" without curlies.

b) Wherever you use a macro parameter, wrap it in parentheses to avoid precedence issues if it is an expression. So:

#define SAFE_SET_PATH(old,new) { NSString* temp = [new retain]; [old release]; old = temp; }

But of course, the smarter venue is always to not use macros for such stuff and instead use functions. IIRC C99 even brought us inline functions, which are a much better choice for such cases.

Macros are best only used when there is no other way to achieve something (e.g. if you need to paste together tokens for identifier names etc.)

Is it really so difficult to write proper accessors the long way, or use a
tool like Accessorizer? The benefit is that you can instantly debug
problems like this. Do you save enough effort using macros for that
practice to pay off?

You could also just turn on the Script menu in xCode. It contains two Perl scripts that take the selected variable declaration and create accessor declarations and definitions from that and put them on the clipboard.
--
Cheers,
M. Uli Kusterer
------------------------------------------------------------
"The Witnesses of TeachText are everywhere..."
http://www.zathras.de
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.


References: 
 >RE: [OT] Retain count riddle (From: "Jonathan E. Jackel" <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.