Created
April 19, 2011 21:48
-
-
Save jkubicek/929792 to your computer and use it in GitHub Desktop.
Generally helpful macros I like to include in all my projects
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
// | |
// HelpfulMacros.h | |
// | |
// Created by Jim Kubicek on 4/19/11. | |
// Copyright 2011 Jim Kubicek. All rights reserved. | |
// | |
// Logging | |
// Based on code from http://cocoaheads.byu.edu/node/6 | |
static inline void QuietLog (NSString *format, ...) { | |
if (format == nil) { | |
printf("nil\n"); | |
return; | |
} | |
// Get a reference to the arguments that follow the format parameter | |
va_list argList; | |
va_start(argList, format); | |
// Perform format string argument substitution, reinstate %% escapes, then print | |
NSString *s = [[NSString alloc] initWithFormat:format arguments:argList]; | |
printf("%s\n", [[s stringByReplacingOccurrencesOfString:@"%%" withString:@"%%%%"] UTF8String]); | |
va_end(argList); | |
} | |
#ifndef LocationLog | |
#define LocationLog(format,...) do {\ | |
{ \ | |
NSString *file = [[NSString stringWithUTF8String:__FILE__] lastPathComponent]; \ | |
printf("%s:%d - ", [file UTF8String], __LINE__); \ | |
QuietLog((format),##__VA_ARGS__); \ | |
}\ | |
} while(0) | |
#endif | |
// Error checking | |
#define LOG_ERROR_IF_FALSE_OR_NULL(x) if(!x) LocationLog(@"%@", error) | |
// Check to see if an object is empty or null | |
// http://blog.wilshipley.com/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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment