Reputation: 4551
I have seen in some source code (by other developers) something like this:
#import "SomeClass+SomeOtherClass.h"
What is the +
for? What does this mean?
Upvotes: 3
Views: 137
Reputation: 22820
The "+" in header/source filenames is - by convention - used to describe Category
implementations.
Example :
Let's say you want to add some functionality to an existing class (e.g.the NSString
class). (NSString+Utilities.h
)
// NSString+Utilities.h
@interface NSString (Utilities)
-(NSString *) doSthWithThisString;
@end
// NSString+Utilities.m
@implementation NSString (Utilities)
-(NSString *) doSthWithThisString
{
NSMutableString *transformedStr = [self copy];
// Do sth
return transformedStr;
}
@end
Using it :
// in another file
#import "NSString+Utilities.h"
- (void)awakeFromNib
{
NSString* myString = @"This is a string";
// you may use our new NSString method as much as any already-existing one
NSString* newString = [myString doSthWithThisString];
}
Reference :
Upvotes: 4
Reputation: 27536
Let's say you want to add functionality to an existing class (exp: NSString
). You can do that by creating a subclass or you can use a category. And it is common to name the file where the category is defined using the pattern : MyClass+MyCategory.h
.
For example, we can add a method reverseString
to the class NSString
in a category:
// File NSString+reversable.h
- (NSString *)reverseString;
// File NSString+reversable.m
- (NSString *)reverseString
{
// Implementation
}
Have a look at this documentation for more information about categories.
Then you can use that category in another class:
#import "NSString+reversable.h"
// ...
NSString *aString = @"Hello!";
NSString *reversedString = [aString reverseString];
Upvotes: 4