Search code examples
objective-ccore-graphics

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


I have a CGSize property in a class and I need to check if it has been initialized. I know that a CGSize isn't a object, but generally speaking, is the same idea of checking if an object is different from nil. How to do that?


Solution

  • 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.