In Objective C, there's a Class structure which acts as a pointer to any class implementations you have. In Objective C, it's defined as:

typedef struct objc_class *Class;

I'm trying to have a array of Classes and I'm trying to iterate through the Class array and perform static functions. Some sample code below:

//Each class is subclassed from the same abstract class with a function specialStaticFunction
NSArray *array = [[NSArray alloc] initWithObjects:Class1, Class2, Class3];

for(Class tempClass in array)
    [tempClass specialStaticFunction];
}

When running that code, I get warning because not every Class has a function called specialStaticFunction. So I want to know how to cast a Class or how else someone might circumvent this warning.

有帮助吗?

解决方案

Use id instead of Class here

for(id tempClass in array)

This should remove the warnings from the compiler, but you still need to ensure each object implements that method otherwise your app will crash.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top