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.

4 Answers 4

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.

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.

1

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

4
  • 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 Oct 9, 2011 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. Oct 10, 2011 at 7: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. Oct 10, 2011 at 9: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). Oct 11, 2011 at 7: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.

2
  • Thanks for your input. I have updated a question with more details. Looking forward to your ideas .)
    – Eimantas
    Oct 9, 2011 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, 2011 at 0:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.