我有一个类,我想在 Cocoa 和 Cocoa Touch 应用程序之间共享。该类计算各种CGPoints 并将它们存储在一个数组中。在 Cocoa 中有[NSValue valueWithPoint]. 在 Cocoa Touch 中有[NSValue valueWithCGPoint].
是否有任何解决方案可以让我绕过这些特定于框架的方法?
我有一个类,我想在 Cocoa 和 Cocoa Touch 应用程序之间共享。该类计算各种CGPoints 并将它们存储在一个数组中。在 Cocoa 中有[NSValue valueWithPoint]. 在 Cocoa Touch 中有[NSValue valueWithCGPoint].
是否有任何解决方案可以让我绕过这些特定于框架的方法?
您可以创建一个类别NSValue,以便将valueWithCGPoint:和CGPointValue方法添加到 Cocoa。
#如果 !TARGET_OS_IPHONE @interface NSValue (MyCGPointAddition) + (NSValue *)valueWithCGPoint:(CGPoint)point; - (CGPoint)CGPointValue; @结尾 #万一
#如果 !TARGET_OS_IPHONE
@implementation NSValue (MyCGPointAddition)
+ (NSValue *)valueWithCGPoint:(CGPoint)point {
返回 [self valueWithPoint:NSPointFromCGPoint(point)];
}
- (CGPoint)CGPointValue {
返回 NSPointToCGPoint([self pointValue]);
}
@结尾
#万一
或者您可以使用valueWithBytes:objCType:and getValue:。
CGPoint 点 = {10, 10};
NSValue *value = [NSValue valueWithBytes:&point objCType:@encode(CGPoint)];
// ...
[值 getValue:&point];