Do you often write smthng like this in ObjC?
__weak __typeof(self) weakSelf = self;
self.completionHandler = ^{
__typeof(self) strongSelf = weakSelf;
[strongSelf doSomething];
// or just use weakSelf
weakSelf.var = nil;
// some other code
self.property = @"foo";
};
It looks horrible. And, did you notice, we forgot to use weakSelf when set foo. It's really often mistake. So, with ODWeakify you can do it easier:
// #import <ODWeakify.h>
od_weakify(self);
self.completionHandler = ^{
od_strongify(self);
[self doSomething];
self_weak_.var = nil; // We can still use weak if we need it
self.property = @"foo";
};
Now all self
s inside block will be in safe from retain cycles.
And if you forget strongify (and won't use self_weak_) you'll get a warning.
ODWeakify supports multiple methods for installing the library in a project.
CocoaPods is a dependency manager for Objective-C and Swift, which automates and simplifies the process of using 3rd-party libraries like ODWeakify in your projects. You can install it with the following command:
$ gem install cocoapods
To integrate ODWeakify into your Xcode project using CocoaPods, specify it in your Podfile
:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
target 'TargetName' do
pod 'ODWeakify'
end
Then, run the following command:
$ pod install
Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.
You can install Carthage with Homebrew using the following command:
$ brew update
$ brew install carthage
To integrate ODWeakify into your Xcode project using Carthage, specify it in your Cartfile
:
github "Rogaven/ODWeakify" ~> 1.1
Run carthage
to build the framework and drag the built ODWeakify.framework
into your Xcode project.
Alexey Nazarov, alexx.nazaroff@gmail.com
ODWeakify is available under the MIT license. See the LICENSE file for more info.