3

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?

Update in regard to answers

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.

Eimantas
  • 48,927
  • 17
  • 132
  • 168

4 Answers4

4

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.

Paul Lynch
  • 19,769
  • 4
  • 37
  • 41
3

Since you are adding methods on runtime, so you should also invoke them with runtime function, objc_msgSend or performSelector:withObject:for example, so the compiler will not going to warn you anything.

xuzhe
  • 5,100
  • 22
  • 28
1

Well, if you call them, you know their signature, and if you know their signature, you can declare them, can't you?

Jean-Denis Muys
  • 6,772
  • 7
  • 45
  • 71
  • Your update doesn't seem enough. *if you can call* them, you can declare them. So show us how you call them, so we can understand – Jean-Denis Muys Oct 09 '11 at 15:32
  • 1
    The fact that a method is called does not imply the knowledge of the signature at compile time and therefore it does not imply the declarability of this method. – Tilo Prütz Oct 10 '11 at 07:41
  • You are right of course. But the OP says there is a warning from the compiler. I don't see why the OP asks about how to get rid of a warning without letting us know which warning, and on which source code line. What's the point of withholding that information? It might very well be relevant. – Jean-Denis Muys Oct 10 '11 at 09:28
  • 1
    Indeed the question would be much less misleading with this information. Nevertheless the question _“how can I convince compiler that the class will respond to undeclared (generated) methods and make it not throw warnings?”_ gives the hint that the warning is about undeclared methods (e.g. foo may not respond to bar). – Tilo Prütz Oct 11 '11 at 07:08
0

Declare this method in a category for NSObject and make an empty implementation:

@interface NSObject (DynamicMethodsCategory)

- (void)doSomething;

@end

@implementation NSObject (DynamicMethodsCategory)

- (void)doSomething
{
}

@end

In your object you can call it without any warnings:

@implementation MyObject

- (void)someMethod
{
    [self doSomething];
}

@end

Then generate implementation of [MyObject doSomething] dynamically, it will be called instead of NSObject's one.

Update: Alternatively, the method can be declared in a category for the object. This suppresses the compiler's Incomplete implementation warning. However, I think this is not a good workaround, because the application will crash if the method is not created dynamically in runtime before it is called.

Davyd Geyl
  • 4,578
  • 1
  • 28
  • 35
  • Thanks for your input. I have updated a question with more details. Looking forward to your ideas .) – Eimantas Oct 09 '11 at 11:41
  • So, do you know the name of the method at compile time or not? If not, how can you make the compiler to warn you? – Davyd Geyl Oct 10 '11 at 00:29