0

i got a UIViewCustom Class with 7 childs. each of the childs got their own class functions for helping initiating

+(int) minHeight;
+(int) minWidth;

in a UITableView i select one of the classes and the function "-insertNewObjectWithClassName:(NSString*)childClassName" gets called.

In that function i want to create the instance depending to the classname, so i tryed

Class *class = NSClassFromString(childClassName);
CGRect frame = CGRectMake(0, 0, [class minWidth], [class minWidth])
MotherClass *view = [[class alloc] initWithFrame:frame];

But unfortunately the static function can't be called.

Is there a way, to say the compiler that class is not just a Class but also a MotherClass to tell him the function?

thank you very much!

EDIT: Warning: Semantic Issue: Method '-minWidth' not found (return type defaults to 'id')

SOLUTION: Class class instead of Class *class

Seega
  • 3,001
  • 2
  • 34
  • 48

3 Answers3

3

Something must be wrong elsewhere, e.g. the name of the class you are passing. This demo program works as expected (layout compacted for brevity):

#import <Foundation/Foundation.h>

@interface MyChild
+ (int) minHeight;
+ (int) minWidth;
@end

@implementation MyChild
+ (int) minHeight { return 100; }
+ (int) minWidth { return 300; }
@end

int main(int argc, const char *argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSString *className = [NSString stringWithString: @"MyChild"];
    Class theClass = NSClassFromString(className);
    NSLog(@"%d %d", [theClass minHeight], [theClass minWidth]);

    [pool drain];
    return 0;
}

Output:

2011-08-10 18:15:13.877 ClassMethods[5953:707] 100 300
Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
  • Hi, i used Class * theClass = NSClassFromString(className); So the Pointer was the Problem – Seega Aug 11 '11 at 06:57
2

This answer seems related: How do I call +class methods in Objective C without referencing the class?

You can define an interface that has the methods you want to call and then have something like:

Class<MyCoolView> class = NSClassFromString(childClassName);
CGRect frame = CGRectMake(0, 0, [class getMinWidth], [class getMinWidth]);
MotherClass *view = [[class alloc] initWithFrame:frame];

This should eliminate compiler warnings and make your code typesafe.

Community
  • 1
  • 1
Sam
  • 26,946
  • 12
  • 75
  • 101
  • THANK You you are my HERO! The warning has left – Seega Aug 10 '11 at 16:03
  • That'll work, but a protocol is overkill. I'd go with an abstract base class and simply declare `class` to be `MyAbstractBase* class`. This is cleaner and more indicative of intention; protocols are for functionality spread across the class hierarchy. In this case, all of you custom classes are rooted with a single base subclass of `UIView` (I'm guessing UIView). – bbum Aug 10 '11 at 16:08
  • I agree that it seems like overkill, but I don't know of a better way in this case. I don't think code like `MyAbstractBase * class = NSClassFromString(@"someClassName")` will work since the return type is Class and MyAbstractBase* should point at an instance of a MyAbstractBase* and not the class type. – Sam Aug 10 '11 at 16:21
2
  • Objective-C does not have static functions. It has Methods; class or instance.

  • do not prefix methods with get; that is reserved for a particular use case, this isn't it.

Your code looks more or less correct. Are you sure that childClassName contains the correct name of the class?

In general, your question indicates a bit of a lack of an understanding of Objective-C or, at the least, an assumption that Obj-C works like C++ (or Java). I would suggest perusing the language documentation as it will answer various meta questions that will make all of this pretty clear.

bbum
  • 162,346
  • 23
  • 271
  • 359
  • 1
    @Seega: not class **functions**. They are **methods**. The way they are invoked is not the same as how functions are called. – Rudy Velthuis Aug 10 '11 at 16:00