Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the macros in Objective-C / Cocoa?

I'm coming from a place without macros (Java/Python/C#/Scala) so perhaps my perspective is distorted but...

Why are macros used in Cocoa? Two that spring to mind are NSLocalizedString and NSAssert (and STAssert). Would it be so hard / unsuitable to make them functions (which could be inlined)? I suppose I find them a little bizarre as an unnecessary throw-back to C (and yes, I am familiar with the pedigree of Obj-C).

Is it just something that was done back in the day or is there a specific reason?

like image 358
Joe Avatar asked Dec 18 '22 00:12

Joe


1 Answers

In general it is preferable to avoid macros, but for functionality like that provided by NSAssert(), you can't.

If you look at the definition, you'll see that it uses __LINE__, __FILE__, _cmd and self from the context it is used in. If it wasn't a macro, it wouldn't have access to that information.

For NSLocalizedString however, i see no obvious reason.

like image 61
Georg Fritzsche Avatar answered Dec 23 '22 12:12

Georg Fritzsche