I stumbled upon https://github.com/AlanQuatermain/aqtoolkit/blob/master/Extensions/NSObject%2BProperties.h
The interface is defined as:
@interface NSObject (AQProperties)
What does the (AQProperties)
part do?
I stumbled upon https://github.com/AlanQuatermain/aqtoolkit/blob/master/Extensions/NSObject%2BProperties.h
The interface is defined as:
@interface NSObject (AQProperties)
What does the (AQProperties)
part do?
They are categories - a great way to extend existing classes without subclassing them.
To sum up, your NSObject (AQProperties)
defines an NSObject
with some extra methods that pertains to AQProperties
.
Unlike a subclass, you can only add methods to a category, not an extra data member.
It's called a "category". You can extend existing classes, even if you do not have the source code for them, by adding your methods to a named category:
@interface NSString (MyFancyStuff)
- (NSString*) stringByCopyingStringAndReplacingDollarCharsWithEuros;
- (NSUInteger) lengthIgnoringWhitespace;
@end
You can then implement these new methods as usual
@implementation NSString (MyFancyStuff)
- (NSString*) stringByCopyingStringAndReplacingDollarCharsWithEuros
{
...
}
- (NSUInteger) lengthIgnoringWhitespace
{
...
}
@end
These new methods then behave like all other methods of NSString
(provided, you did include the header file, where you declared the category):
NSString* text = [NSString stringWithFormat: @"Hello, %@", param];
NSUInteger length = [text lengthIgnoringWhitespace];
...
Apart from being a tool to extend classes, whose code you don't have, categories can also be useful to structure the interfaces of your own classes. For example:
FancyStuff.h
@interface FancyStuff
// Public methods and properties are declared here
@end
FancyStuff+Protected.h
#import "FancyStuff.h"
@interface FancyStuff (Protected)
// Methods, which may be useful for classes, which derive from
// FancyStuff, but should not be considered part of the public API
@end
FancyStuff.m
#import "FancyStuff+Protected.h"
@implementation FancyStuff
...
@end
@implementation FancyStuff (Protected)
...
@end