Weakify and Strongify Macros for Objective-C
I worked with Chat to hammer these out. Not sure why everybody on the Internet suggests typeof
but anyway...
The correct syntax is indeed __typeof(), not typeof(). The __typeof() keyword is a GCC extension which is supported by both GCC and LLVM/Clang compilers. It allows you to declare a variable of the same type as another variable.
And so...
// weakify: creates a new weak reference 'weakSelf' to the variable (helps avoid retain cycles)
// Usage: weakify(self)
#define weakify(var) __weak __typeof(var) weakSelf = var;
// strongify: creates a new strong reference 'strongSelf' from the weak reference (ensures object stays in memory during block execution)
// Usage: strongify(weakSelf)
#define strongify(var) __strong __typeof(var) strongSelf = var;