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

為{幸葍}努か 提交于 2019-12-04 08:23:20

问题


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


回答1:


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 @.




回答2:


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)


来源:https://stackoverflow.com/questions/20861922/meaning-of-objective-c-macros-prefixed-with-an-at-symbol

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!