2014-01-18 51 views
-2

是否有可能在Objective-C中创建一个CGPoint数组,但我写了这段代码,但它导致了一个错误!从NSMultableArray到CGPoint

NSMultableArray *result=[[NSMultableArray alloc]initWithCapacity:10]; 
CGPoint temp=[Self GetPoint]; 
result[0]=temp; //Error !! 

回答

1

是的,你需要嵌入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 
} 

这里有一个linkNSValue UIKit的添置类参考

1

不,您不能在数组中存储CGPoint。数组只存储指针类型对象。你必须包裹起来CGPointNSValue然后将其存储在阵列..

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

1

您可以通过 [result addObject:[NSValue valueWithCGPoint:player.center]];

和解码如下

CGPoint point = [(NSValue *)[result objectAtIndex:0] CGPointValue];