Search Your Question

Difference between category in Objective C and extension Swift?

Ans : 

Differences between Objective-C Category and Extension


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.
When to use : 
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 .


Difference between Objective-C category and Swift extension :

Mostly Objective-C category and Swift extension are same. 

Diff : 

In Objective-C category, Computed property/variable can not be declared.
In Swift extension, Computed property(not stored property) can be declared.


No comments:

Post a Comment

Thanks