是否有可能在Objective-C中创建一个CGPoint数组,但我写了这段代码,但它导致了一个错误!从NSMultableArray到CGPoint
NSMultableArray *result=[[NSMultableArray alloc]initWithCapacity:10];
CGPoint temp=[Self GetPoint];
result[0]=temp; //Error !!
是否有可能在Objective-C中创建一个CGPoint数组,但我写了这段代码,但它导致了一个错误!从NSMultableArray到CGPoint
NSMultableArray *result=[[NSMultableArray alloc]initWithCapacity:10];
CGPoint temp=[Self GetPoint];
result[0]=temp; //Error !!
是的,你需要嵌入CGPoint
s在NSValue
对象内,如下所示:
NSMutableArray *arr = [[NSMutableArray alloc] init];
NSValue *val1 = [NSValue valueWithCGPoint:cgpoint1];
[arr addObject:val1];
// etc.
提取CGPoint
S,使用此:
for (NSValue *value in arr) {
CGPoint cgpoint = value.pointValue;
// Use point
}
这里有一个link到NSValue UIKit的添置类参考。
不,您不能在数组中存储CGPoint
。数组只存储指针类型对象。你必须包裹起来CGPoint
到NSValue
然后将其存储在阵列..
NSMultableArray *result=[[NSMultableArray alloc]initWithCapacity:10];
NSValue *pointvalue = [NSValue valueWithCGPoint:CGPointMake(x, y)];
[result addObject:pointvalue];
在检索时
CGPoint *myPoint = [[result objectAtIndex:indexValue] CGPointValue];
而且你可以用其他的结构类型也https://developer.apple.com/library/ios/documentation/uikit/reference/NSValue_UIKit_Additions/Reference/Reference.html
您可以通过 [result addObject:[NSValue valueWithCGPoint:player.center]];
和解码如下
CGPoint point = [(NSValue *)[result objectAtIndex:0] CGPointValue];
CGPoint是一个结构类型做到这一点,你只能存储Objective-C对象进入可变数组。做最好的办法是用它来包装它NSValue像现在这样[NSValue valueWithCGPoint:temp]
类似的问题在这里:For iPad/iPhone apps, how to have an array of points?