What does this mean in Objective-C
-
13-12-2019 - |
Question
I have a file called ManageAddressBook.m The file has two @implementation inside
What does the second one mean?
@implementation ManageAddressBook
@end
@implementation NSString (Additions)
@end
Solution
The first implementation is just a new class. The second implementation is basically extension methods to an existing class, known as Categories. See the article: Categories and Extensions for more information.
As summarized at the top of the link:
A category allows you to add methods to an existing class—even to one for which you do not have the source. Categories are a powerful feature that allows you to extend the functionality of existing classes without subclassing. Using categories, you can also distribute the implementation of your own classes among several files. Class extensions are similar, but allow additional required APIs to be declared for a class in locations other than within the primary class @interface block.
OTHER TIPS
The second one implements an Objective-C Category on NSString
, i.e. methods that operate on NSString
, but are not provided by the writers of NSString
class.
Adding categories lets you extend functionality of a class in a way that is somewhat similar to C#'s extension methods: the objects start responding to the messages defined in categories, making it convenient for you to write functionality in a flexible and modular way.