How do I check if a CGPoint has been initialised?

后端 未结 6 1821
你的背包
你的背包 2021-02-07 02:59

I would like to initially set a CGPoint property to a particular point (middle of screen). Other methods may subsequently wish to change this property. My thoughts were to initi

6条回答
  •  一向
    一向 (楼主)
    2021-02-07 03:26

    A CGPoint is a struct, so you can't set it to nil or NULL (it's not a pointer). In a sense, there's really no "uninitialized" state. Perhaps you could use {0.0, 0.0} to designate an unset CGPoint, but that's also a valid coordinate. Or you could use negative x and y values to flag an "uninitialized" point, since negative values can't be valid drawing points, but that's a bit of a hack, too.

    Probably your best bet is to do one of two things:

    1. Store the property as a pointer to a CGPoint. This value can be set to NULL when uninitialized. Of course, you have to worry about mallocing and freeing the value.
    2. Store the CGPoint alongside a BOOL called pointInitialized or somesuch, initially set to NO, but set to YES once the point has been initialized. You can even wrap that up in a struct:

      struct {
          CGPoint point;
          BOOL initialized;
      } pointData;
      

提交回复
热议问题