NSClassFromString返回nil [英] NSClassFromString returns nil

查看:431
本文介绍了NSClassFromString返回nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么 NSClassFromString return nil ?根据定义,它必须返回类名。
我应该如何解决这个问题?我需要从字符串实例化一个类,并调用方法,这是在类中,使用创建的实例。



这是我的代码看起来像:

  id myclass = [[NSClassFromString(@Class_from_String)alloc] init]; 
[myclass method_from_class];

但是不会调用 method_from_class ,控制是不会进去的。我的代码是无错误的。任何想法如何解决这个在Objective-C?

解决方案

函数的文档说


返回值

命名的类对象aClassName或nil if没有类通过
名称当前加载。如果
aClassName是nil,返回nil。


如何正确使用这个例子如下: p>

 类dictionaryClass = NSClassFromString(@NSMutableDictionary); 
id object = [[dictionaryClass alloc] init];
[object setObject:@FooforKey:@Bar];


Why does NSClassFromString return nil ? As per the definition it has to return class name. How should I take care to rectify this problem? I need to instantiate a class from string and call the method, which is in the class, using the instance created.

This is how my code looks like:

id myclass = [[NSClassFromString(@"Class_from_String") alloc] init];
[myclass method_from_class];

But the method_from_class function is not being called, control is not going into it. And my code is error free. Any idea how to solve this in Objective-C?

解决方案

The Documentation for the function says:

Return Value The class object named by aClassName, or nil if no class by that name is currently loaded. If aClassName is nil, returns nil.

An example of how this should be properly used is as follows:

Class dictionaryClass = NSClassFromString(@"NSMutableDictionary");
id object = [[dictionaryClass alloc] init];
[object setObject:@"Foo" forKey:@"Bar"];

这篇关于NSClassFromString返回nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆