What is an import statement where the filename contains "+"?
Let's say you want to add functionality to an existing class (exp:) NSString
. You can do this by creating a subclass or you can use a category. And usually called file, where the category is determined by using a template: MyClass+MyCategory.h
.
For example, we can add a method reverseString
to a class NSString
in a category:
// File NSString+reversable.h
- (NSString *)reverseString;
// File NSString+reversable.m
- (NSString *)reverseString
{
// Implementation
}
Take a look at the documentation for more information on categories.
Then you can use this category in another class:
#import "NSString+reversable.h"
// ...
NSString *aString = @"Hello!";
NSString *reversedString = [aString reverseString];
source to share
The "+" in header / source names is - by convention - used to describe implementations Category
.
Example:
Let's say you want to add some functionality to an existing class (like a class NSString
). ( 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:
// 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];
}
Link:
source to share