In Ruby, there is a very useful function called tap, that “taps” into method calls and run your code without interfering with the method chain. There is an Objective C port called NSObject-Tap.
Here's how you might write some initialization code without using tap:
Person* person = [[Person alloc] init];
person.name = @"Your Name";
person.age = 20;
person.address = @"Kyoto, Japan";
And with NSObject-Tap
:
Person* person = [[[Person alloc] init] tap:^(Person *p) {
p.name = @"Your Name";
p.age = 20;
p.address = @"Kyoto, Japan";
}];
-tap:
runs the block passing in self and returns self again so you can add -tap:
calls into a method chain easily. It can help to reduce the need for certain local variables as well as shorten code.
There's also a -tapp
method that prints out itself like so:
NSString* helloWorld = [[@"Hello, world!" tapp] uppercaseString];
Similar to -tap:
, -tapp
can be chained because it returns self.
Your feedback is valuable: Do you want more nuggets like this? Yes or No
.
.