如何在 Objective-C 中构建 CGPoint 数组?
我想要得到这个结构:
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),
};
以便进行一些计算并绘制数据。
我有 CGPoint *lines = appDelegate.averageResponseTimePoints;
如何从 *lines
制作数组 addLines[]
?
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;
How to make array addLines[]
from *lines
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对于 iOS:
创建数组:
获取第一个 CGPoint 对象:
For iOS:
Create array:
Get 1st CGPoint object:
C 数组在运行时并不是真正的数组,它们只是指向相同类型的连续对象块的指针。 当您看到
items[n]
时,这只是*(items+n)
的语法糖。在您的示例中,
addLines[1]
将是*(lines+1)
,而addLines[0]
将是*(lines+ 0)
,即*lines
。 因此,addLines
只是没有指针取消引用的lines
。*lines
是数组中的第一项,lines
是整个数组。数组在编译时与指针有一些区别。 例如,
sizeof(addLines)
将为您提供整个数组的大小。一旦将数组传递到其大小可能可变的地方,数组性就会丢失,但您仍然可以使用下标运算符。 例如:
这在我的 Mac 上输出以下内容:
这里,
square
是四个CGPoint
的大小,但一旦发送到pass_back
函数,它只是指针的大小,因为它就是这样。 当指针返回(并命名为returned
)时,它仍然可以像数组一样使用。请注意循环中的幻数
4
。 指针不知道它所指向的数组的长度。无法使用
=
运算符重新分配数组。 如果您确实必须使用lines
中的点填充addLines
,您可以使用如下所示的方法来实现:您必须获取
numberOfPoints
从某个地方,addLines 必须足够大才能处理这些点。 如果点数是一个常量,那没问题,但如果点数在运行时可能变化,特别是如果点来自外部世界(想想任意代码执行),那就不好了。我将更改
averageResponseTimePoints
以返回 NSArray 而不是 C 样式数组。 您需要将 CGPoint 封装在对象中 - 您自己的对象或 NSValue 。以下是如何编写
averageResponseTimePoints
的示例:您的代码使用 CocoaTouch 运行,您可以使用它来创建点值:从
如果 数组,你可以这样写:
或者使用 CocoaTouch:
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)
.In your example
addLines[1]
would be*(lines+1)
andaddLines[0]
would be*(lines+0)
, which is*lines
. So,addLines
is justlines
without the pointer dereference.*lines
is the first item in the array andlines
is the whole array.Arrays have some differences to pointers at compile time. For example,
sizeof(addLines)
would give you the size of the whole array.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:
This outputs the following on my Mac:
Here,
square
is the size of fourCGPoint
s, but once sent to thepass_back
function, it's only the size of a pointer, because that's what it is. When the pointer comes back (and namedreturned
) it can still be used like an array.Note the magic number
4
in the loop. The pointer doesn't know the length of the array it's pointing to.Arrays cannot be reassigned with the
=
operator. If you really must populateaddLines
with the points fromlines
, you can do that with something like the following:You'll have to get
numberOfPoints
from somewhere, andaddLines
will have to be large enough to handle those points. That's okay if the number of points is a constant, but it would be bad if the number of points can vary at run time, especially if the points come from the outside world (think arbitrary code execution).I'd change
averageResponseTimePoints
to return an NSArray rather than a C-style array. You'll need to encapsulate theCGPoint
s in objects - either your own object orNSValue
s.Here's an example of how you could write
averageResponseTimePoints
:If your code runs with CocoaTouch, you can use this to create the point value instead:
To get the
CGPoint
s out of the array, you could write something like this:Or with CocoaTouch:
试试这个
它有效。 祝你好运。
Try this
It works. Good Luck.
我尝试了 Tibidabo 的答案,但它对我不起作用。 它说 [myCGPointArray objectAtIndex:0] 是 id 类型,所以我无法对其调用 CGPointValue。 嗯。
但这有效:
或者
我在这里找到它: 如何以简单的方式将 CGPoint 对象添加到 NSArray?,由 Jarret Hardie 发布。
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.
this worked however:
or
I found it here: How can I add CGPoint objects to an NSArray the easy way?, posted by Jarret Hardie.
我解决了接下来的问题:
感谢 michael-jensen 的 解决方案!
I solve my problem doing the next:
Thanks to michael-jensen for the solution!