0

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?

Duck
  • 34,902
  • 47
  • 248
  • 470
  • there's no CGPointValue method on NSValue class. https://developer.apple.com/library/mac/documentation/cocoa/reference/foundation/classes/NSValue_Class/Reference/Reference.html – Andrey Chernukha May 28 '14 at 11:46
  • @HutinPuylo There is in the [iOS rendition](https://developer.apple.com/library/ios/documentation/uikit/reference/NSValue_UIKit_Additions/Reference/Reference.html). – Rob May 28 '14 at 11:49
  • 1
    @HutinPuylo it is here: https://developer.apple.com/library/ios/documentation/uikit/reference/NSValue_UIKit_Additions/Reference/Reference.html#//apple_ref/occ/instm/NSValue/CGPointValue – Bryan Chen May 28 '14 at 11:50

3 Answers3

2

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
Bryan Chen
  • 45,816
  • 18
  • 112
  • 143
  • ahhh, ok, but out of curiosity, why Xcode is not giving me any error when I compile for Cocoa? – Duck May 28 '14 at 11:50
  • @RubberDuck I don't know... maybe somehow Xcode think `self` is `id` (untyped) so no type check was performed – Bryan Chen May 28 '14 at 11:51
1

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
Bluedays
  • 151
  • 5
-2
+ (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];
Plokstorm
  • 27
  • 1
  • No. This [is a Cocoa method](https://developer.apple.com/library/ios/documentation/uikit/reference/NSValue_UIKit_Additions/Reference/Reference.html). You cannot just change the name. The issue is that it's an instance method, not a class method. – Rob May 28 '14 at 11:51