【问题标题】:Creating a c-array of CGPoints创建 CGPoints 的 c 数组
【发布时间】:2013-09-18 15:06:02
【问题描述】:

我想在 c 样式数组中创建一个 CGPoints 常量数组。

我从这个开始但收到错误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),
};

我删除了static,认为这可能与此有关,但错误仍然存​​在。

如何创建 CGPoints 数组(更广泛地说,任何类似定义的结构)。


注意:我已经发布了这个问题和答案,部分原因是为了我自己的参考,因为我永远无法记住这一点,并且浪费太多时间从其他来源研究答案。这里希望它可以帮助别人!

【问题讨论】:

    标签: ios c arrays struct


    【解决方案1】:

    事实证明,CGPointMake 函数调用是“不是编译时间常数”的东西,因此需要将 CGPoints 视为原始结构:

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

    演员表不是严格要求的,但为了我自己的理智,我会保留它以显示每个数字实际上是 CGPoint 的一部分。这也是有效的:

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

    【讨论】:

      【解决方案2】:

      调用函数始终是运行时活动。数组初始值设定项列表的内容需要在编译时计算。

      【讨论】:

      • 调用非constexpr 函数始终是运行时活动。调用 constexpr 函数可能是编译时的事情,具体取决于上下文和/或优化设置。
      • @rubenvb constexpr 在 C 中不存在,问题被标记为。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      • 2015-09-15
      • 1970-01-01
      • 2013-02-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多