views:

792

answers:

3

I write an instance method in ClassName.m:

-(void)methodName:(paraType)parameter
{...}

And call it using

[self methodName:parameter]; 
A warning will pop up, but the code still runs successfully.

Is this because I haven't created an instance of the class? Why the method still runs normally? And what is the correct way to call self method to prevent the warning?

+1  A: 

What is the warning that you get?

Do you have a definition of the method in your header file?

The syntax you use is the propper way of calling method on self.

bjartek
The problem may be basic, but this troubles me a lot. I really forget to define the method in head file. Thanks indeed.
iPhoney
+5  A: 

Well the first step in receiving help with a warning would be to post the warning :)

I am assuming it is something about an unrecognized message? If so it's because although the compiler sees the call to "methodName" it does not know if that is valid for the object or not.

I would guess your code looks like;

-(void) someFunc
{
  ...
  [self methodName:parameter]; 
  ...
}

-(void)methodName:(paraType)parameter
{
...
}

You can either;

a) Place the 'methodName' function earlier in the file so the compiler has seen it before it's used in calls.

b) declare it in the class interface. E.g.

// Foo.h
@interface Foo {
...
}
-(void) methodName:(paraType)parameter;
@end
Andrew Grant
+1  A: 

The method will work because Objective-C methods are resolved at run-time. I expect the warning you get is something like "Object Foo may not respond to -methodName:" and then it tells you that it's defaulting the return type to id. That's because the compiler hasn't seen a declaration or definition of -methodName: by the time it compiles the code where you call it. To remove the warning, declare the method in either the class's interface or a category on the class.

Graham Lee