When would you use Categories over Inheritance and vice versa?
When would you use Categories over Inheritance and vice versa?
Here is my attempt at answering your Objective-C interview question:
Categories in Objective-C allow you to add methods to an existing class without subclassing. This is useful in a few key scenarios:
Extending system classes - You can add methods to Apple's built-in classes like NSString or UIView to add functionality that is missing. This allows you to extend the capabilities of these classes without modifying the original source code.
Organizing code - You can split a class's implementation into multiple categories, each containing related methods. This helps keep your code organized and modular.
Providing optional functionality - Categories allow you to provide optional methods that a class can adopt if needed, without forcing all users of the class to include the extra code.
Unit testing - Categories make it easy to isolate and test specific methods of a class in unit tests, by putting those ...
middle
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào