Incorrect in lens c

Is there a way to make a nullable struct in a C object, as in C # you can use Nullable<T>?
I need to CGPointbe nullwhen there is no applicable value. I cannot allocate a random invalid value for this type (-5000, -5000), because all values ​​are valid for this.

+3
source share
4 answers

What if you define CGPoint using CGPointMake (NAN, NAN) similar to CGRectNull? Of course, with NAN for coordinates, this is not a valid point.

+8
source

CGPoint is a structure and has several different rules in objective-c than you think. You should consider reading about structs in objective-c.

, , - , null. NSValue CGPoint.

NSValue * v = [NSValue valueWithPoint:CGPointMake(1,9)];
NSVAlue * vNull = [NSValue valueWithPointer:nil];
if([v objCType] == @encode(CGPoint)) printf("v is an CGPoint");
+3

CGPoint is an enumeration, not an object. You can use CGPointZero, or you can wrap all your points inside NSValue, which are objects and can be null.

+1
source

There is also nothing to prevent you from creating your own CGPoint-based framework, similar to how C # 2 works.

struct NilableCGPoint { bool isNil; CGPoint point; }

Examples of using:

// No value (nil)
NilableCGPoint myNilablePoint.point = CGPointZero;
myPoint.isNil = YES;

// Value of (0,0)
NilableCGPoint myNilablePoint.point = CGPointZero;
myPoint.isNil = NO;

// Value of (100, 50)
NilableCGPoint myNilablePoint.point = CGPointMake(100, 50);
myPoint.isNil = NO;
0
source

All Articles