Created
December 8, 2008 18:27
-
-
Save mkhl/33550 to your computer and use it in GitHub Desktop.
Objective-C macros
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// GCC Attribute for autoscoped Obj-C objects | |
// Source: http://www.cocoabuilder.com/archive/message/cocoa/2009/3/13/232287 | |
#define autoscoped __attribute__((cleanup(releaseObject))) | |
static inline void releaseObject(id *object) | |
{ | |
[*object release]; | |
} | |
- (void) demo | |
{ | |
autoscoped NSArray *anArray = [[NSArray alloc] init]; | |
// do something with anArray | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A quick check if an object is empty | |
// Source: http://www.wilshipley.com/blog/2005/10/pimp-my-code-interlude-free-code.html | |
static inline BOOL isEmpty(id thing) { | |
return (thing == nil) | |
|| ([thing respondsToSelector:@selector(length)] | |
&& [(NSData *)thing length] == 0) | |
|| ([thing respondsToSelector:@selector(count)] | |
&& [(NSArray *)thing count] == 0); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Helpful macros for object creation | |
// Source: http://cbarrett.tumblr.com/post/53371495/some-helpful-macros-for-object-creation | |
#define NSARRAY(...) [NSArray arrayWithObjects: __VA_ARGS__, nil] | |
#define NSDICT(...) [NSDictionary dictionaryWithObjectsAndKeys: __VA_ARGS__, nil] | |
#define NSSET(...) [NSSet setWithObjects: __VA_ARGS__, nil] | |
#define NSBOOL(_X_) ((_X_) ? (id)kCFBooleanTrue : (id)kCFBooleanFalse) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Use Unions (instead of type-punning) to recast Structs. | |
// Source: http://cocoawithlove.com/2008/04/using-pointers-to-recast-in-c-is-bad.html | |
#define UNION_CAST(x, destType) (((union {__typeof__(x) a; destType b;})x).b) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment