1

I have a universal app and for each device (iPad, iPhone) have different sets of properties like this:

//iPhone
@property (nonatomic, strong) CategoriesViewController *categoriesViewController;
//iPad
@property (nonatomic, strong) UIPopoverController *categoriesPopoverController;

But I don't want to waste memory synthesizing properties for each device. Like in my case when user opens app with iPhone, only iPhone specific @properties should be created. Also wice-versa for app running on iPad.

Is there any directive in compile time to defer this, something like that:

#if UI_USER_INTERFACE_IDIOM() = iPhone
//create iPhone properties
#elseif UI_USER_INTERFACE_IDIOM() = iPad
//create iPad properties

Is there any way to do this or is there a better way to handle this?

Thanks!

Borut Tomazin
  • 8,041
  • 11
  • 78
  • 91

2 Answers2

5

There is no way to do exactly what you're asking.

  1. Is this extra memory usage really significant? A property like that is only 4 bytes per instance of your object. If it's in something like a controller, where you have only one (or a few) instances, then this is not even worth talking about.

  2. If you really need to do this, consider making an abstract base class, then make subclasses for the iPad and iPhone versions. At runtime, use UI_USER_INTERFACE_IDIOM() to decide which class to instantiate.

Kurt Revis
  • 27,695
  • 5
  • 68
  • 74
  • I know that property itself isn't a big deal. But what happens when this property is synthesized? – Borut Tomazin Mar 08 '12 at 09:56
  • Nothing magic. The compiler generates a getter (and maybe setter) method for the property. While more than 4 bytes, the code and related symbol information still doesn't add up to much. Compared to the size of the typical images in an app, it's totally insignificant. – Kurt Revis Mar 08 '12 at 10:02
3

This is standard Objective C way to solve such issues:

  1. Create @protocol reflecting common behavior of both controllers.
  2. Both controllers should conform this protocol, i.e implement behavior.
  3. Create ONE @property like:

    @property (nonatomic, retain) id < YourProtocolName > categoriesController.

  4. Instantiate iPad or iPhone related controller into categoriesController.

This make your architecture more elegant. Have a nice day ;)

SVGreg
  • 2,320
  • 1
  • 13
  • 17
  • One more thing: this also make you app more universal due to you can inject ANY controller conforming you @protocol into this property and it will work. – SVGreg Mar 08 '12 at 10:56