0

我正在尝试为 Cocoa 和 iOS 创建一个跨平台的 NSValue 类别,它将处理 CGPoint/NSPoint 和 CGSize/NSSize 等。

我有这个:

#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

Mac 部分运行良好,但 iOS 部分给我一个错误

  return (CGPoint)[self CGPointValue];

有两条消息:1)没有已知的选择器 CGPointValue 的类方法和“使用类型 CGPoint(又名 struct CGPoint),其中需要算术或指针类型。

这是为什么?

4

3 回答 3

2

因为+[NSValue CGPointValue]不存在你想要的-[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
于 2014-05-28T11:48:28.093 回答
1

为了让事情变得更简单,只需创建一个仅适用于 iOS 的类别并使用与 OS X 上相同的方法名称,因为对于 OS X CGPoint 和 NSPoints 是相同的,你不需要更多,你不必修改你的 OS X 代码。

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

- (CGPoint) pointValue {
    return [self CGPointValue];
}
#endif
@end
于 2015-01-08T13:02:01.700 回答
-2
+ (CGPoint) getPoint {
  return (CGPoint)[self CGPointValue];
}

您的变量以 maj 开头?用 min 设置变量不是更好吗?这可能是 CGPoint 类的问题。

[self cgPointValue];
于 2014-05-28T11:44:40.990 回答