简体   繁体   English

对类参数执行类方法

[英]perform class method on class parameter

How can I perform a class method on a class passed to a method? 如何在传递给方法的类上执行类方法?

Let's say I have a class: 假设我有一堂课:

@interface MyClass:NSOBject
+ (NSArray *) array;
@end

When I instantiate the class 当我实例化课程时

MyClass test = [[MyClass alloc] init;

I can access the array by 我可以通过访问数组

[[test class] array];

but if I pass this class to a method 但是如果我将此类传递给方法

[doStuffWithMyClass: (Class) aClass];

how can I access the arry within the method? 如何在方法中访问arry? can I "cast" class to a certain class? 我可以“上课”到某个班吗?

- (NSInteger) doStuffWithMyClass: (Class) aClass {
   return [aClass array].count;
}

Class es are objects, so yes, you can cast them. es Class是对象,所以是的,您可以强制转换它们。

Otherwise you can just do 否则你可以做

[aClass performSelector:@selector(array)];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM