简体   繁体   English

NSClassFromString(MyClassName) 比调用 MyClass 的 class function

[英]NSClassFromString(MyClassName) than calling class function of MyClass

i got a UIViewCustom Class with 7 childs.我有一个 UIViewCustom Class 有 7 个孩子。 each of the childs got their own class functions for helping initiating每个孩子都有自己的 class 函数来帮助启动

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

in a UITableView i select one of the classes and the function "-insertNewObjectWithClassName:(NSString*)childClassName" gets called.在 UITableView 我 select 类之一和 function "-insertNewObjectWithClassName:(NSString*)childClassName" 被调用

In that function i want to create the instance depending to the classname, so i tryed在那个 function 我想根据类名创建实例,所以我尝试了

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.但不幸的是,无法调用 static function。

Is there a way, to say the compiler that class is not just a Class but also a MotherClass to tell him the function?有没有办法让编译器说 class 不仅仅是一个 Class 也是一个 MotherClass 来告诉他 function?

thank you very much!非常感谢!

EDIT: Warning: Semantic Issue: Method '-minWidth' not found (return type defaults to 'id')编辑:警告:语义问题:找不到方法“-minWidth”(返回类型默认为“id”)

SOLUTION: Class class instead of Class *class解决方案:Class class 代替 Class *class

Something must be wrong elsewhere, eg the name of the class you are passing.其他地方一定有问题,例如您传递的 class 的名称。 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: Output:

2011-08-10 18:15:13.877 ClassMethods[5953:707] 100 300

This answer seems related: How do I call +class methods in Objective C without referencing the class?这个答案似乎相关: How do I call +class methods in Objective C without reference 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.这应该消除编译器警告并使您的代码类型安全。

  • Objective-C does not have static functions. Objective-C 没有 static 功能。 It has Methods;它有方法; class or instance. class 或实例。

  • do not prefix methods with get ;不要在方法前面加上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?您确定childClassName包含 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).一般来说,您的问题表明对 Objective-C 缺乏了解,或者至少假设 Obj-C 的工作方式类似于 C++(或 Java)。 I would suggest perusing the language documentation as it will answer various meta questions that will make all of this pretty clear.我建议仔细阅读语言文档,因为它将回答各种元问题,这些问题将使所有这些都非常清楚。

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

相关问题 在NSClassFromString()返回的类上调用类方法 - Calling class method on a Class returned by NSClassFromString() NSClassFromString 行为异常和缓存类 - NSClassFromString misbehaving and caching class 我得到一个NSClassFromString的null类 - I get a null class with NSClassFromString 为什么在OS X上有[@“”class]!= NSClassFromString(NSStringFromClass([@“”class]))? - Why is [@“” class] != NSClassFromString(NSStringFromClass([@“” class])) on OS X? 目标C“ @class MyClassName”的C ++等效项是什么? - What is the C++ equivalent of Objective C “@class MyClassName”? 在 Swift 中动态实例化 ObjC class(使用 NSClassFromString?) - Dynamically instanciating ObjC class in Swift (with NSClassFromString?) 如何执行从NSClassFromString强制转换的类的类方法? - How to perform a class method of a class which is casted from NSClassFromString? 由NSClassFromString生成的类不与头文件中定义的类关联 - Class resulted from NSClassFromString does not associate with class defined in header file NSClassFromString 返回一个 Class object 不能正确响应 isSubClassOfClass - NSClassFromString returns a Class object which doesn't respond to isSubClassOfClass properly 调用类的成员函数? - Calling a member function of a class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM