我正在尝试创建一个跨平台的NSValue类别,它将处理Cocoa和iOS的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];
}
#endifMac部分工作得很好,但是iOS部分给了我一个错误
return (CGPoint)[self CGPointValue];有两条消息: 1)没有已知的选择器CGPointValue类方法和需要算术或指针类型的已使用类型CGPoint (也称为struct CGPoint)。
为什么会这样呢?
发布于 2014-05-28 11:48:28
因为+[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发布于 2015-01-08 13:02:01
为了使事情更简单,只需创建一个只适用于iOS的类别,并使用与OS相同的方法名,因为对于OS,CGPoint和NSPoints是相同的,您不需要更多,也不必修改OS代码。
@implementation NSValue (XCross)
#if TARGET_OS_IPHONE
+ (NSValue *) valueWithPoint:(CGPoint)point {
return [NSValue valueWithCGPoint:point];
}
- (CGPoint) pointValue {
return [self CGPointValue];
}
#endif
@end发布于 2014-05-28 11:44:40
+ (CGPoint) getPoint {
return (CGPoint)[self CGPointValue];
}你的变量是从maj开始的?用min来设置变量不是更好吗。这可能是CGPoint类的一个问题。
[self cgPointValue];https://stackoverflow.com/questions/23910621
复制相似问题