Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the convention for prefixing categories with magic words?

Convention with categories is to include a prefix when extending Apple classes. For example:

[string XXDataUsingUTF8];

(Yes, it's a stupid example. Just go with it.)

What I'm less clear on is what the convention is when the identifier starts with copy, set, init or new.

For instance:

[request setHTTPBodyWithKeyValues: keyValues];

If I add my prefix, this no longer follows the "setters start with set" convention.

[request XXSetHTTPBodyWithKeyValues: keyValues];

On the other hand, if I add the prefix after the set, it's no longer really a prefix:

[request setXXHTTPBodyWithKeyValues: keyValues];

This isn't terribly import with set, but I think it becomes more important with init, copy, etc.

Has Apple documented this anywhere? What's in common use?

like image 896
Steven Fisher Avatar asked Mar 03 '12 17:03

Steven Fisher


1 Answers

You could set the method family explicitly in the interface declaration:

@interface NSObject (StevenFisherCategory)

- (NSData *)XXCopyDataUsingUTF8 __attribute__((objc_method_family(copy)));

@end
like image 119
rob mayoff Avatar answered Oct 08 '22 12:10

rob mayoff