Objective-C Tips & Trick - Prioregroup.com
[Objective-C] More useful macros
       
back

// Devices
#define ISPAD() ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
#define ISPHONE() ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
#define ISPHONE5() (([[UIScreen mainScreen] bounds].size.height-568)?NO:YES)
#define ISRETINA() (BOOL isRetina = NO; if ([UIScreen instancesRespondToSelector:@selector(scale)])) { isRetina = ([[UIScreen mainScreen] scale] == 1.0 ? NO : YES) } return isRetina)
// #define DEVICE_ID [[UIDevice currentDevice] uniqueIdentifier] deprecated!!

// Path
#define DOC_PATH(path)[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:path]

// iOS Versions
#define SYSTEM_VERSION_EQUAL_TO(v)                  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v)              ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)

// WebView
#define SCROLL_ENABLE(enable,webview) {for(id subview in webview.subviews){if ([[subview class] isSubclassOfClass:[UIScrollView class]]){((UIScrollView *)subview).scrollEnabled=enable;}}}
#define BOUNCES_ENABLE(enable,webview)  {for(id subview in webview.subviews){if ([[subview class] isSubclassOfClass:[UIScrollView class]]){((UIScrollView *)subview).bounces=enable;}}}

// AlertView
#define ALERT(title,msg) {UIAlertView* alert = [[[UIAlertView alloc] initWithTitle:title message:msg delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:nil] autorelease];[alert show];}

// Frame Position
#define CGPositionMake(r,x,y) CGRectMake(x,y,r.frame.size.width,r.frame.size.height)

// Localization
#define LocalizedString(key) [[NSBundle mainBundle] localizedStringForKey:(key) value:@"" table:nil]

// Block
#ifdef __clang__
/* clang doesn't support always_inline attribute on blocks,
   use -O3 or other aggressive inlining optimization, say -finline-functions */
	#define _ALWAYS_INLINE  
#else
	#define _ALWAYS_INLINE  __attribute__((always_inline))
#endif
#define __with_v(t) ^(t v) _ALWAYS_INLINE { #define __with_end(v) } (v);
/* example (origin: http://pastebin.com/JpUHNgX4)
	__with_v(UILabel*)
		v.textColor = [UIColor darkGrayColor];
		v.font = [UIFont systemFontOfSize:13.];
	 __with_end(v.textLabel);
*/

// ARC Trick
#define ARC_RETAIN(obj) ((id(*)(id, SEL))objc_msgSend)(obj, NSSelectorFromString(@"retain"));
#define ARC_RELEASE(obj) ((id(*)(id, SEL))objc_msgSend)(obj, NSSelectorFromString(@"release"));






Copyright © 1996-2018 Centro Studi Informatica di Danilo Priore. All rights reserved. P.I.10149810581.
prioregroup.com