How to check if a CGSize is initialized (or if its value is different from "nil")

You can compare it to CGSizeZero or an arbitrary size that you consider invalid.

if (!CGSizeEqualToSize(CGSizeZero, mySize)) {
   // do something
}

It depends on what you mean by "in a class". If this is an instance variable, your problems are over, because you are guaranteed that an instance variable will be auto initialized to some form of zero (i.e. CGSizeZero). But if you just mean "in my code somewhere", e.g. an automatic variable, then there is no such test; it is entirely up to you to initialize before use, and until you do, the value could be anything at all (sorry, but that's how C works).

On the whole, your question is itself a "bad smell". If it matters to you at some point in your code whether this value has been initialized, then you are doing it wrong. It's your value; it was up to you to initialize it (e.g. when your overall object was initialized). Or, if for some reason you need to know whether your setter has ever been called, then you need to add a boolean to your setter that tells whether it has ever been called.


CGSize is a C struct. With few exceptions (when used as an iVar), there is no guarantee for its initialized value. It could be anything, especially when created on the stack.

Thus, you are responsible for initializing it properly, and since "zeros" are valid values, there is no guaranteed way to tell if it was set to "zero" on purpose, or it has been uninitialized.