开发者

How to init array in objective-c?

开发者 https://www.devze.com 2023-03-10 05:07 出处:网络
I want to use an array in all 开发者_如何学Cmethods of my class. The array is initialized in the init method of the class.

I want to use an array in all 开发者_如何学Cmethods of my class. The array is initialized in the init method of the class.

But the size of the array is first known in the init method. E.g. in my init method I have:

CGPoint mVertices[size][size];

later in init I fill the array and in another method I read the values. How can I declare the array globally?


Make it an ivar:

@interface myClass : NSObject {
  CGPoint *mVertices;
}

@end

In your init method:

mVertices = malloc(size * size * sizeof(CGPoint));
if (!mVertices) { return nil; }

In your dealloc method:

free(mVertices); mVertices = NULL;
0

精彩评论

暂无评论...
验证码 换一张
取 消