I have a class that I would like to share between a Cocoa and Cocoa Touch applications. This class calculates various CGPoints and stores them in an array. In Cocoa there’s [NSValue valueWithPoint]. In Cocoa Touch there’s [NSValue valueWithCGPoint].
Is there any solution that would allow me to bypass using these framework-specific methods?
You may create a category on
NSValuein order to add thevalueWithCGPoint:andCGPointValuemethods to Cocoa.#if ! TARGET_OS_IPHONE @implementation NSValue (MyCGPointAddition) + (NSValue *)valueWithCGPoint:(CGPoint)point { return [self valueWithPoint:NSPointFromCGPoint(point)]; } - (CGPoint)CGPointValue { return NSPointToCGPoint([self pointValue]); } @end #endifOr you may use
valueWithBytes:objCType:andgetValue:.CGPoint point = {10, 10}; NSValue *value = [NSValue valueWithBytes:&point objCType:@encode(CGPoint)]; // ... [value getValue:&point];