objective-c 如何在Objective C中形成CGPoint数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/943945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to form CGPoint array in Objective C
提问by Mladen
I want to get this structure
我想得到这个结构
CGPoint addLines1[] =
{
CGPointMake(30.0, 150.0),
CGPointMake(41.67, 145.19),
CGPointMake(53.33, 103.25),
CGPointMake(65.0, 131.67),
CGPointMake(76.67, 106.11),
CGPointMake(88.33, 110.20),
CGPointMake(100.0, 111.54),
CGPointMake(111.67, 112.13),
CGPointMake(123.33, 115.66),
CGPointMake(135.0, 123.7),
CGPointMake(146.67, 125.53),
CGPointMake(158.33, 115.1),
CGPointMake(170.0, 69.38),
CGPointMake(181.67, 112.47),
CGPointMake(193.33, 65.1),
CGPointMake(205.0, 103.33),
CGPointMake(216.67, 92.6),
CGPointMake(228.33, 54.76),
CGPointMake(240.0, 79.66),
CGPointMake(251.67, 53.81),
CGPointMake(263.33, 56.81),
CGPointMake(275.0, 88.19),
CGPointMake(286.67, 74.81),
CGPointMake(298.33, 28.1),
CGPointMake(310, 20.0),
};
In order to make some calculations and draw data.
为了进行一些计算和绘制数据。
I have CGPoint *lines = appDelegate.averageResponseTimePoints;
我有 CGPoint *lines = appDelegate.averageResponseTimePoints;
How to make array addLines[]from *lines?
如何使阵列addLines[]从*lines?
回答by Tibidabo
For iOS:
对于 iOS:
Create array:
创建数组:
NSArray *myCGPointArray = @[[NSValue valueWithCGPoint:CGPointMake(30.0, 150.0)],[NSValue valueWithCGPoint:CGPointMake(41.67, 145.19)]];
Get 1st CGPoint object:
获取第一个 CGPoint 对象:
CGPoint myPoint = [myCGPointArray[0] CGPointValue];
回答by Will Harris
C arrays are not really arrays at run time, where they are just pointers to a contiguous block of objects of the same type. When you see items[n], that's just syntactic sugar for *(items+n).
C 数组在运行时并不是真正的数组,它们只是指向相同类型的连续对象块的指针。当您看到时items[n],这只是*(items+n).
In your example addLines[1]would be *(lines+1)and addLines[0]would be *(lines+0), which is *lines. So, addLinesis just lineswithout the pointer dereference. *linesis the first item in the array and linesis the whole array.
在您的示例addLines[1]中将是*(lines+1)并且addLines[0]将是*(lines+0),即*lines. 所以,addLines只是lines没有指针取消引用。*lines是数组中的第一项并且lines是整个数组。
Arrays have some differences to pointers at compile time. For example, sizeof(addLines)would give you the size of the whole array.
数组在编译时与指针有一些不同。例如,sizeof(addLines)会给你整个数组的大小。
Array-ness is lost as soon as you pass the array somewhere where it's size might be variable, but you can still use the subscript operator. For example:
一旦您将数组传递到其大小可能可变的地方,数组性就会丢失,但您仍然可以使用下标运算符。例如:
#include <Foundation/Foundation.h>
#define showsize( expr ) ( printf(#expr " = %zd\n", ( expr ) ) )
CGPoint *
pass_back(CGPoint points[4])
{
showsize(sizeof(points));
return points;
}
int
main(void)
{
CGPoint square[] = {CGPointMake(-1.0, 1.0),
CGPointMake( 1.0, 1.0),
CGPointMake( 1.0, -1.0),
CGPointMake(-1.0, -1.0)};
CGPoint* returned;
int i;
showsize(sizeof(CGPoint));
showsize(sizeof(CGPoint*));
showsize(sizeof(square));
returned = pass_back(square);
showsize(sizeof(returned));
for (i = 0; i < 4; ++i) {
printf("returned[%d] = {%0.1f, %0.1f}\n", i, (float) returned[i].x,
(float) returned[i].y);
}
return 0;
}
This outputs the following on my Mac:
这会在我的 Mac 上输出以下内容:
sizeof(CGPoint) = 8
sizeof(CGPoint*) = 4
sizeof(square) = 32
sizeof(points) = 4
sizeof(returned) = 4
returned[0] = {-1.0, 1.0}
returned[1] = {1.0, 1.0}
returned[2] = {1.0, -1.0}
returned[3] = {-1.0, -1.0}
Here, squareis the size of four CGPoints, but once sent to the pass_backfunction, it's only the size of a pointer, because that's what it is. When the pointer comes back (and named returned) it can still be used like an array.
这里,square是四个CGPoints的大小,但是一旦发送到pass_back函数,它只是一个指针的大小,因为它就是这样。当指针返回(并命名returned)时,它仍然可以像数组一样使用。
Note the magic number 4in the loop. The pointer doesn't know the length of the array it's pointing to.
注意4循环中的幻数。指针不知道它指向的数组的长度。
Arrays cannot be reassigned with the =operator. If you really must populate addLineswith the points from lines, you can do that with something like the following:
不能使用=运算符重新分配数组。如果您确实必须addLines使用 from 中的点进行填充lines,您可以使用以下内容进行操作:
memcpy(addLines, lines, sizeof(CGPoint) * numberOfPoints);
You'll have to get numberOfPointsfrom somewhere, and addLineswill have to be large enough to handle those points. That's okay if the number of points is a constant, but it would be badif the number of points can vary at run time, especially if the points come from the outside world (think arbitrary code execution).
您必须numberOfPoints从某个地方出发,并且addLines必须足够大才能处理这些点。如果点数是常数,那没关系,但是如果点数在运行时会发生变化,特别是如果点来自外部世界(想想任意代码执行),那就不好了。
I'd change averageResponseTimePointsto return an NSArray rather than a C-style array. You'll need to encapsulate the CGPoints in objects - either your own object or NSValues.
我会更改averageResponseTimePoints为返回 NSArray 而不是 C 样式的数组。您需要将CGPoints封装在对象中 - 无论是您自己的对象还是NSValues。
Here's an example of how you could write averageResponseTimePoints:
这是一个如何编写的示例averageResponseTimePoints:
- (NSArray*) averageResponseTimePoints
{
NSMutableArray* result = [[[NSMutableArray alloc] init] autorelease];
for (int i = 0; i < numberOfPoints; ++i) {
NSValue* point = [NSValue value:points+i
withObjCType:@encode(CGPoint)];
[result addObject:point];
}
return result;
}
If your code runs with CocoaTouch, you can use this to create the point value instead:
如果您的代码使用 CocoaTouch 运行,您可以使用它来创建点值:
NSValue* point = [NSValue valueWithCGPoint:points[i]];
To get the CGPoints out of the array, you could write something like this:
要从CGPoint数组中取出 s,您可以这样写:
for (NSValue* value in result) {
NSPoint pt;
[value getValue:&pt];
NSLog(@"%f %f", pt.x, pt.y);
}
Or with CocoaTouch:
或者使用 CocoaTouch:
CGPoint pt = [value CGPointValue];
回答by Baruch Kly
Try this
尝试这个
CGPoint A[10];
A[0] = CGPointMake(10.0, 10.0);
A[1] = CGPointMake(10.0, 12.0);
float x = A[1].y;
NSLog(@"%.0f", x);
It works. Good Luck.
有用。祝你好运。
回答by juggler
I tried Tibidabo's answer, and it didn't work for me. It said [myCGPointArray objectAtIndex:0] was of type id, so I couldn't call CGPointValue on it. hm.
我尝试了 Tibidabo 的答案,但对我不起作用。它说 [myCGPointArray objectAtIndex:0] 是 id 类型,所以我不能在它上面调用 CGPointValue。嗯。
this worked however:
然而,这有效:
NSArray *points = [NSArray arrayWithObjects:
[NSValue valueWithCGPoint:CGPointMake(5.5, 6.6)],
[NSValue valueWithCGPoint:CGPointMake(7.7, 8.8)],
nil];
NSValue *val = [points objectAtIndex:0];
CGPoint point = [val CGPointValue];
float X = point.x;
NSLog(@"cgpoint value is: %f", X);
or
或者
NSArray *points = [NSArray arrayWithObjects:
[NSValue valueWithCGPoint:CGPointMake(20, 6.6)],
[NSValue valueWithCGPoint:CGPointMake(7.7, 8.8)],
nil];
float xCoordinate = [[points objectAtIndex:0] CGPointValue].x;
I found it here: How can I add CGPoint objects to an NSArray the easy way?, posted by Jarret Hardie.
我在这里找到了:How can I add CGPoint objects to an NSArray the simple way? ,由 Jarret Hardie 发布。
回答by IgniteCoders
I solve my problem doing the next:
我解决了我的问题做下一个:
NSMutableArray *myArray = [[NSMutableArray alloc] init];
[myArray addObject:[NSValue valueWithCGPoint:myPoint]];
Thanks to michael-jensenfor the solution!

