Return to Snippet

Revision: 59149
at August 21, 2012 04:34 by yar


Initial Code
#define IS_PHONE  UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPhone

#define ALLOC_PER_DEVICE()  id retVal = nil; \
                            NSString *className = NSStringFromClass(self);\
                            if (IS_PHONE && ![className hasSuffix:@"Phone"]) {\
                                className = [NSString stringWithFormat:@"%@Phone", className];\
                                Class newClass = NSClassFromString(className);\
                                retVal = [newClass alloc];\
                            }\
                            if (!retVal)\
                                retVal = [super alloc];\
                            assert(retVal != nil);\
                            return retVal\

// Sample Usage
+ (id) alloc {
    ALLOC_PER_DEVICE();
}

Initial URL


Initial Description
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."

Initial Title
Alloc Method to Choose Between Regular and Phone

Initial Tags
iphone, ios

Initial Language
Objective C