我有一个我想在Cocoa和Cocoa Touch应用程序之间共享的课程。此类计算各种CGPoint并将它们存储在数组中。在可可粉中有[NSValue valueWithPoint]。在Cocoa Touch中,有一个[NSValue valueWithCGPoint]

有什么解决方案可以让我绕过这些特定于框架的方法?

最佳答案

您可以在NSValue上创建类别,以便将valueWithCGPoint:CGPointValue方法添加到Cocoa。

#if! TARGET_OS_IPHONE
@interface NSValue(MyCGPointAddition)
+(NSValue *)valueWithCGPoint:(CGPoint)point;
-(CGPoint)CGPointValue;
@结束
#万一

#if! TARGET_OS_IPHONE
@implementation NSValue(MyCGPointAddition)
+(NSValue *)valueWithCGPoint:(CGPoint)point {
    返回[self valueWithPoint:NSPointFromCGPoint(point)];
}
-(CGPoint)CGPointValue {
    返回NSPointToCGPoint([self pointValue]);
}
@结束
#万一

或者您可以使用valueWithBytes:objCType:getValue:

CGPoint点= {10,10};
NSValue * value = [NSValue valueWithBytes:&point objCType:@encode(CGPoint)];
// ...
[值getValue:&point];

关于cocoa-touch - 在 cocoa 和 cocoa touch 的NSArray中存储CGPoint的通用解决方案?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11438166/

10-11 22:21
查看更多