-2

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?

jd.
  • 4,057
  • 7
  • 37
  • 45

2 Answers2

1

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.

Nirav Bhatt
  • 6,940
  • 5
  • 45
  • 89
1

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
Dirk
  • 30,623
  • 8
  • 82
  • 102
  • OK. I then declare an interface of the sort: #import "NSObject+Properties.h" @interface GFDictionaryInitiable : NSObject But when I try to access a method defined in my Category, I get the following: -[GFRestaurant hasPropertyNamed:]: unrecognized selector sent to instance. GFRestaurant is an instance of GFDictionaryInitiable. What am I missing? – jd. Oct 17 '13 at 16:10
  • Is `GFRestaurant` an **instance** of `GFDictionaryInitiable` or a **subclass**? Reading the error message, `GFRestaurant` seems to be a subclass. – Dirk Oct 17 '13 at 16:15
  • A subclass: @interface GFRestaurant : GFDictionaryInitiable – jd. Oct 17 '13 at 16:16
  • Could you provide more context? Show some code? – Dirk Oct 17 '13 at 16:17
  • I have a first class, @interface GFDictionaryInitiable : NSObject. I want all subclasses of GFDictionaryInitiable, such as GFRestaurant (@interface GFRestaurant : GFDictionaryInitiable) to also have all methods impletemented in NSObject+Properties.h – jd. Oct 17 '13 at 16:19
  • Show us some code... Besides, since `GFDicitionaryInitable` inherits from `NSObject`, it should inherit the methods in categories on `NSObject`, too. Just to make sure: is the file implementing `NSObject (Properties)` part of your Xcode project, and properly linked into the executable? – Dirk Oct 17 '13 at 16:23
  • Creating a new question, with proper code examples. Thanks for your help. – jd. Oct 17 '13 at 16:24
  • See: http://stackoverflow.com/questions/19432324/objective-c-how-to-inherit-category-methods-defined-in-a-parent-class – jd. Oct 17 '13 at 16:32