Day 6: Difference between class category and class extension in Objective-C ?

Max
2 min readJan 29, 2018

--

Photo by Zonduurzaam Deventer on Unsplash
  1. Factor -> Source code

Categories provide a way to add methods to a class even if its source code is not available to you. ex: NSString

Extensions are only possible if the source code is available , because compiler compiles the extension & source code at same time.

2. Factor ->Instance variable/Properties

Category : Not possible

Extensions : Possible

3. Factor ->Accessibility to Inherited classes.

Category — All methods defined inside a category are also available to the inherited class

Extensions — All properties and methods defined inside a class extension are not even available to inherited class.

Bottom Line

Use category when you need to want to add any methods to a class whose source code is not available to you OR you want to break down a class into several files depending on its functionality.

Use class extensions when you want to add some required methods to your class outside the main interface file. Also when you want to modify a publicly declared variable in the main interface file .

References:

That’s one question, see you tomorrow. If you ❤️ this article please give it a clap👏 and support my work. Also, smash that Follow icon 🟢 to get posts delivered to you on your couch. We are in this together 😇. Cheers!

Psst!👋, want to continue? Go to next article in the series or the previous one, or if you wish to start from beginning.

--

--

Max
Max

Written by Max

Senior iOS Engineer | Swift | ObjectiveC | SwiftUI | Combine |

Responses (1)