【问题标题】:Can't withdraw CGPoint Values from A mutableArray of NSValue无法从 NSValue 的 mutableArray 中提取 CGPoint 值
【发布时间】:2012-07-22 23:41:00
【问题描述】:

我创建了一个 CGpoints 的 NSMUtableArray,并通过将它们子类化为 NSValue 来存储它们,但我无法获取这些值。这就是我所做的。

 CGPoint graphPoint;
    NSMutableArray *pointValues = [[NSMutableArray alloc] init];

    for( int x =0;x<5; x++)
    {
    NSDictionary* xValue = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:x] forKey:@"X"];
    graphPoint.x =x;
    graphPoint.y = [CalculatorBrain runProgram: self.graphingPoint usingVariableValues:xValue];

    [pointValues addObject:[NSValue valueWithCGPoint:graphPoint]];

}

我尝试使用它来获取值,但我只是返回 null

for( int x=0; x<5; x++) {
NSValue *val = [pointValues objectAtIndex:x];
CGPoint point = [val CGpointValue];
NSLog(@"Points = %@", point);
}

【问题讨论】:

  • 代码不崩溃真是奇迹。

标签: objective-c xcode nsmutablearray cgpoint nsvalue


【解决方案1】:

您不能使用%@ 格式说明符打印CGPoint,因为它不是对象。相反,请使用NSStringFromCGPoint():

NSLog(@"%@", NSStringFromCGPoint(myPoint));

【讨论】:

  • 似乎有效,但我收到 NSStringFromPoint 的隐式声明警告,可以吗?
  • @TerrelGibson 您是否包含了 UIKit / AppKit?您的代码可能仍然可以正常工作,但该警告永远不会是好的。
  • 我是否只执行 #include "UIKit / AppKit" 因为我收到一条错误消息,提示找不到文件名
  • @TerrelGibson 不,您包括 UIKit/UIKit.h(适用于 iOS)或 AppKit/AppKit.h(适用于 mac)。
  • @JoshCaswell 好点,感谢您指出这一点,已修复。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-08
相关资源
最近更新 更多