Reputation: 32066
I'd like to create a constant array of CGPoints in a c style array.
I started with this but got the error 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.
How can you create an array of CGPoints (and more widely, any similarly defined struct).
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!
Upvotes: 2
Views: 593
Reputation: 409442
Calling a function is always a runtime activity. The contents of an array initializer list needs to be computed at compilation time.
Upvotes: 0
Reputation: 32066
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:
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. This is also valid:
static CGPoint locations[5] = {
{180, 180},
{300, 130},
{435, 120},
{470, 230},
{565, 200},
};
Upvotes: 3