Useful Cocoa Macros


/ Published in: Objective C
Save to your folder(s)

Some macros that I use in many applications, provides an easy way to observe/retrieve preferences and access common application information


Copy this code and paste it in your HTML
  1. #define APP_NAME [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleName"]
  2. #define APP_VERSION [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]
  3. #define OPEN_URL(urlString) [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:urlString]]
  4.  
  5. // Retrieving preference values
  6. #define PREF_KEY_VALUE(x) [[[NSUserDefaultsController sharedUserDefaultsController] values] valueForKey:(x)]
  7. #define PREF_KEY_BOOL(x) [(PREF_KEY_VALUE(x)) boolValue]
  8. #define PREF_SET_KEY_VALUE(x, y) [[[NSUserDefaultsController sharedUserDefaultsController] values] setValue:(y) forKey:(x)]
  9. #define PREF_OBSERVE_VALUE(x, y) [[NSUserDefaultsController sharedUserDefaultsController] addObserver:y forKeyPath:x options:NSKeyValueObservingOptionOld context:nil];
  10.  
  11. /* key, observer, object */
  12. #define OB_OBSERVE_VALUE(x, y, z) [(z) addObserver:y forKeyPath:x options:NSKeyValueObservingOptionOld context:nil];
  13.  
  14. #ifdef __OBJC__
  15. static inline BOOL isEmpty(id thing) {
  16. return thing == nil
  17. || ([thing respondsToSelector:@selector(length)]
  18. && [(NSData *)thing length] == 0)
  19. || ([thing respondsToSelector:@selector(count)]
  20. && [(NSArray *)thing count] == 0);
  21. }
  22. #endif

URL: http://mabblog.com/source.html

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.