Alloc Method to Choose Between Regular and Phone


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

This method will alloc the current class unless the device is a phone (also defined here). Then it will try to alloc the class with the suffix "Phone."


Copy this code and paste it in your HTML
  1. #define IS_PHONE UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPhone
  2.  
  3. #define ALLOC_PER_DEVICE() id retVal = nil; \
  4. NSString *className = NSStringFromClass(self);\
  5. if (IS_PHONE && ![className hasSuffix:@"Phone"]) {\
  6. className = [NSString stringWithFormat:@"%@Phone", className];\
  7. Class newClass = NSClassFromString(className);\
  8. retVal = [newClass alloc];\
  9. }\
  10. if (!retVal)\
  11. retVal = [super alloc];\
  12. assert(retVal != nil);\
  13. return retVal\
  14.  
  15. // Sample Usage
  16. + (id) alloc {
  17. ALLOC_PER_DEVICE();
  18. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.