w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Why do we needed category when we can use a subclass? and Why we needed blocks when we can use functions?


Just reading the documentation "Subclassing Notes" for NSString shows why creating categories is sometimes better than subclassing.

If you wanted to add a function -(void)reverseString (for instance) to NSString then subclassing it is going to be a massive pain in comparison to categories.


Blocks are useful for capturing scope and context. They can also be passed around. So you can pass a block into an asynchronous call which then may be passed elsewhere. TBH you don't care where the block is passed or where it is finally called from. The scope captured at the time of creating the block is captured too.

Yes, you can use methods too. But they both have different uses.

Your questions are a bit odd. It's like asking...

Why do hammers exist when we can just use wrenches?

© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.