Objective-C动态创建的方法和编译器警告 [英] Objective-C dynamically created methods and compiler warnings

查看:87
本文介绍了Objective-C动态创建的方法和编译器警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在运行时动态生成方法然后调用它们-我如何说服编译器该类将响应未声明的(生成的)方法并使其不引发警告?

If I generate methods dynamically on runtime and then call them - how can I convince compiler that the class will respond to undeclared (generated) methods and make it not throw warnings?

有关答案的更新

当我生成方法时-在编译时不知道它们的名称.举个例子-如果我有一个视图控制器MyFooController并且它是用方法initWithFoo:(Foo*)foo启动的,那么我将能够为UINavigationController生成类似pushMyFooControllerWithFoo:(Foo *)foo的方法.因此,您注意到声明这种方法会适得其反.

When I generate the methods - their name is not known at compile time. To give an example - if I have a view controller MyFooController and it's initiated with method initWithFoo:(Foo*)foo, I'd be able to generate method like pushMyFooControllerWithFoo:(Foo *)foo for UINavigationController. Hence you notice that declaring such methods would be counter-productive.

推荐答案

这不能直接回答您的问题,但是如果我正在生成方法名称(大概是从字符串中),我会使用字符串名称来调用它们,因此绕过了编译器警告.

This doesn't directly answer your question, but if I was generating method names (presumably from strings), I would call them using the string names, hence bypassing the compiler warnings.

[fooController performSelector:NSSelectorFromString(@"pushMyFooControllerWithFoo:") withObject:foo];

这样,您将负责生成的方法名称的有效性.

That way you are responsible for the validity of the generated method names.

这篇关于Objective-C动态创建的方法和编译器警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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