-1

I want to use a C-style CGFloat array defined in the didMoveToView method in the touchesBegan method. I can't seem to define it as a property because it is a CGFloat array. Globals don't seem to work either. My array is defined like this:

CGFloat levelMapX[] = {self.frame.size.width/2 ...};

I am using SpriteKit.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140

1 Answers1

0

You can declare a pointer to a CGFloat. Although arrays and pointers are not the same in C, they can often be used in a similar way.

@property (assign, nonatomic) CGFloat* array;

And then use it like an array:

const NSInteger myArraySize = 10 * sizeof(CGFloat);
self.array = malloc(myArraySize);
self.array[0] = 0.42;
self.array[1] = M_PI;

remember to free it so you don't leak memory:

- (void)dealloc {
    free(self.array);
}
Community
  • 1
  • 1
Michał Ciuba
  • 7,876
  • 2
  • 33
  • 59