Cross-platform NSValue category

691 Views Asked by At

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?

3

There are 3 best solutions below

2
On BEST ANSWER

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
1
On
+ (CGPoint) getPoint {
  return (CGPoint)[self CGPointValue];
}

Your variable begin by a maj ? isn't it better to set your variable with min. It may be a problem with CGPoint class.

[self cgPointValue];
0
On

To make things even simpler, just create a category that is only for iOS and use the same method names as on OS X, since for OS X CGPoint & NSPoints are the same you don't need more, and you don't have to modify your OS X code.

@implementation NSValue (XCross)
#if TARGET_OS_IPHONE
+ (NSValue *) valueWithPoint:(CGPoint)point {
    return [NSValue valueWithCGPoint:point];
}

- (CGPoint) pointValue {
    return [self CGPointValue];
}
#endif
@end