How do I check if a CGPoint has been initialised?

后端 未结 6 1830
你的背包
你的背包 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条回答
  •  猫巷女王i
    2021-02-07 03:26

    CGPoint does not have an uninitialized state. However, if you consider the point (0, 0) as uninitialized, you could use

    if (_graphOrigin.x == 0 && _graphOrigin.y == 0)
    {
        ...
    

    This works because when an Objective-C instance is initialized, all its ivar are cleared to bits of zero, which in the CGFloat representation is 0.0.

    (Note: The == is fine here even if the operands are CGFloat because we want to compare with the an exact bit pattern (ignoring the issue of -0))

提交回复
热议问题