ioscocoacgpointcgsizensvalue

Cross-platform NSValue category


I am trying to create a cross-platform NSValue category that will handle CGPoint/NSPoint and CGSize/NSSize etc, for Cocoa and iOS.

I have this:

#ifdef __MAC_OS_X_VERSION_MAX_ALLOWED
// Mac OSX

+ (NSValue *) storePoint:(NSPoint)point {
  return [NSValue valueWithPoint:point];
}

+ (NSPoint) getPoint {
  return (NSPoint)[self pointValue];
}


#else
// iOS

+ (NSValue *) storePoint:(CGPoint)point {
  return [NSValue valueWithCGPoint:point];
}

+ (CGPoint) getPoint {
  return (CGPoint)[self CGPointValue];
}


#endif

The Mac part works perfectly but the iOS part gives me an error on

  return (CGPoint)[self CGPointValue];

with two messages: 1) no known class method for selector CGPointValue and "used type CGPoint (aka struct CGPoint) where arithmetic or pointer type is required.

why is that?


Solution

  • because +[NSValue CGPointValue] does not exist you want -[NSValue CGPointValue]

    #ifdef __MAC_OS_X_VERSION_MAX_ALLOWED
    // Mac OSX
    
    + (NSValue *) storePoint:(NSPoint)point {
      return [NSValue valueWithPoint:point];
    }
    
    - (NSPoint) getPoint { // this should be instance method
      return (NSPoint)[self pointValue];
    }
    
    
    #else
    // iOS
    
    + (NSValue *) storePoint:(CGPoint)point {
      return [NSValue valueWithCGPoint:point];
    }
    
    - (CGPoint) getPoint { // this should be instance method
      return (CGPoint)[self CGPointValue];
    }
    
    
    #endif