Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of Objective-C macros prefixed with an at (@) symbol

The ReactiveCocoa framework makes use of weakify and strongify macros, both of which are preceded by an '@' symbol.

Here's an example (From this file).

- (RACSignal *)rac_textSignal {
        @weakify(self);
        return [[[[RACSignal
                ... 
               ];
}

What is the significance of the at symbol that is a prefix to the macro name? (NOTE: I have checked the macro, and it is called 'weakify', not '@weakify', so it isn't just the macro name!).

The macro itself is defined here:

https://github.com/jspahrsummers/libextobjc/blob/master/extobjc/EXTScope.h#L45

like image 360
ColinE Avatar asked Dec 31 '13 18:12

ColinE


People also ask

What is the at symbol in Objective-C?

The @ symbol is used in a few places in Objective-C, and basically it's a way to signal that whatever it's attached to is special to Objective-C and not part of regular C. This is important when the computer compiles Objective-C code.

What is a macro Objective-C?

Macros can be used in many languages, it's not a specialty of objective-c language. Macros are preprocessor definitions. What this means is that before your code is compiled, the preprocessor scans your code and, amongst other things, substitutes the definition of your macro wherever it sees the name of your macro.


2 Answers

There is no special meaning to macros starting with an @. This is done in libextobjc to make the @weakify and @strongify macros seem more idiomatic with the rest of the language.

Technically, the @ is not part of the macro. The macro is just weakify or strongify. The actual body of the macro, though, is written such that it will not compile unless preceded with an @. This is done by adding an empty @autoreleasepool {} at the beginning of the macro, but stripping off the leading @.

like image 143
BJ Homer Avatar answered Nov 09 '22 17:11

BJ Homer


The @ isn't part of the macro. weakify is defined as:

#define weakify(...) \
    autoreleasepool {} \
    metamacro_foreach_cxt(ext_weakify_,, __weak, __VA_ARGS__)

So @weakify(self) becomes:

@autorelease {} metamacro_foreach_cxt(ext_weakify_,, __weak, self)
like image 7
rmaddy Avatar answered Nov 09 '22 18:11

rmaddy