简体   繁体   English

创建一个CGPoints的c数组

[英]Creating a c-array of CGPoints

I'd like to create a constant array of CGPoints in ac style array. 我想在ac样式数组中创建一个CGPoints常量数组。

I started with this but got the error Initializer element is not a compile time constant 我从这里开始,但是得到了错误Initializer element is not a compile time constant

static CGPoint locations[5] = 
{
    CGPointMake(180, 180),
    CGPointMake(300, 130),
    CGPointMake(435, 120),
    CGPointMake(470, 230),
    CGPointMake(565, 200),
};

I removed the static thinking it might be something to do with that, but the error remained. 我删除了static想法,认为这可能与此有关,但错误仍然存​​在。

How can you create an array of CGPoints (and more widely, any similarly defined struct). 如何创建CGPoints数组(更广泛地说,是任何类似定义的结构)。


NB: I've posted this question and answer partially for my own reference as I can never remember this off the top of my head and waste too much time researching the answer from other sources. 注意:我已经部分张贴了这个问题和答案,以供我自己参考,因为我永远都记不起这个问题,并且浪费了太多时间来研究其他来源的答案。 Here's hoping it helps others! 希望对其他人有所帮助!

It turns out the CGPointMake function call is the thing that "isn't a compile time constant" and so the CGPoints need to be treated like raw structs: 事实证明, CGPointMake函数调用是“不是编译时间常数”,因此CGPoints必须像原始结构一样对待:

static CGPoint locations[5] = 
{
    (CGPoint){180, 180},
    (CGPoint){300, 130},
    (CGPoint){435, 120},
    (CGPoint){470, 230},
    (CGPoint){565, 200},
};

The cast isn't strictly required, but for my own sanity, I'd keep it to show each of those numbers is actually part of a CGPoint. 强制转换并非严格要求,但出于我自己的理智,我会保留它以显示每个数字实际上都是CGPoint的一部分。 This is also valid: 这也是有效的:

static CGPoint locations[5] = {
    {180, 180},
    {300, 130},
    {435, 120},
    {470, 230},
    {565, 200},
};

Calling a function is always a runtime activity. 调用函数始终是运行时活动。 The contents of an array initializer list needs to be computed at compilation time. 数组初始化器列表的内容需要在编译时计算。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM