开发者

objective-c I want to know the syntax for passing arrays of pointer to a class method

I want to know the syntax for passing arrays of pointer to a class method.

Myclass 开发者_开发问答*arr[TOTAL];

what is the syntax for the class method to take arr?


It is extremely atypical to pass a language array full of Objective-C objects or classes to a method.

I'd suggest:

+ (void) classMethod: (NSArray  *) arrayOfClasses;

And:

[MyClass classMethod: [NSArray arrayWithObjects: [Foo class], [Bar class], nil]];


If the number of elements in the array is fixed, then

+ (void)someMethod:(MyClass *[])array;

is sufficient. Otherwise, pass the number of elements in the array:

+ (void)someMethod:(MyClass *[])array count:(NSUInteger)count;

In order to use the methods above,

MyClass *array[TOTAL];
array[0] = …;
array[1] = …;
…
array[TOTAL - 1] = …;

[MyClass someMethod:array];

or

[MyClass someMethod:array count:TOTAL];


A class method starts with a +. Making it take an array as a parameter is done the same way as you would do it with any other pointer-taking method/function in C or Objective-C:

+ (void)myClassMethod:(MyClass **)arrayOfMyClassPointers;

It is a little strange to be using a C-style array to hold pointers to objects, though - what are you really trying to do?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜