Why does xcode sometimes find instance methods not declared in header files? - iphone

Of course it is best practice to declare all methods in the header file, so I appreciate when xcode warns me than an instance method may not be found. However, there are cases when I have not declared a method in the header, and I do not get a warning. These are definitely not any delegate methods, so what other cases would cause this behavior?

Probably the method has already been defined in the implementation by the time it is used. i.e. if the method being used is above the place it's used in the implementation file then the compiler knows the method signature so all is OK.

if your method is not declared in the header file (or a class extension), but comes before another method which is referencing it then you won't get an error.

If you call your method under your method body definition it works, like:
-(void)foo { bla }
[self foo];
If you too the other way around it crashes (if the method it not in your header file):
[self foo];
-(void)foo { bla }

Related

Iphone Error "request for member connectWeb in something not a structure or union"

When I call my urlconnection method from my (IBAction)buttonpressed method like this:
[self connectWeb];
I get error "request for member connectWeb in something not a structure or union"
but when I call the same method from my - (void)viewDidLoad method it works!?
Where is the connectWeb method in your implementation file?
I would guess that it is before viewDidLoad but after buttonPressed.
The reason for this is that the connectWeb method has been declared before viewDidLoad, so viewDidLoad is aware of it, whereas it's after the buttonPressed method, so the buttonPressed method isn't aware of it.
You have a couple of options.
Declare the method before any other method uses it.
- (void)connectWeb;
Then you can implement it anywhere within the implementation.
Move the implementation of connectWeb to before both viewDidLoad and buttonPressed - both the methods will then be aware of connectWeb.
A method missing from the header wouldn't cause this problem. Because Objective-C is dynamic, it will check if the instance implements that method at runtime, so as long as the method exists it will work. You would just get a compiler warning at build time.
Try deleting the line [self connectWeb] from your button delegate method and copy/pasting it from your viewDidLoad (or retyping it). From the error you're getting, it sounds like there might be an extra character in your statement. This happens to me from time to time, because I use synergy to share my keyboard & mouse between multiple computers.

Where is the code behind this method

Yes all, another newbie question!
I'm doing some maintenance work on an app I inherited and I'm trying to find the code behind this method. In my task.h file I have a optional method, doTask, declared in a protocol, TaskDelegate. In my task.m file I located the method definition for doTask, but it refers back to it's method name so I assumed I'd find another "more complete" method definition in another object. WRONG! I'm obviously missing something very basic here. I haven't been able to find any other references in my code to doTask.
Here's the initial declaration in my header file, task.h
#protocol TaskDelegate<NSObject>
#optional
- (void) doTask;
Here's the method definition in my implementation file, task.m
- (void) doTask
{
if ((self.delegate != nil) && ([self.delegate respondsToSelector:#selector(doTask)]))
{
[self.delegate doTask];
}
}
I assume my method definition is first determining if I have a object delegate already in existence and then seeing if it has a doTask method defined within it; if it does, then it's telling it to execute the doTask method on that delegate. Am I correct?
Well, if so, my question becomes where is the code behind doTask which actually does something? I've really got to be missing something basic here. All help is appreciated! Thanks in advance for your assistance...
As middaparka said, your assumption is spot-on. As for finding the function code for the doTask method, do a Edit -> Find -> Find in Project or command + shift + F and enter 'doTask' (or whatever the function name really is) into the search bar and set it to 'In Project' and it will pull up all instances where the function text shows up, including the function definition.
I assume my method definition is first
determining if I have a object
delegate already in existence and then
seeing if it has a doTask method
defined within it; if it does, then
it's telling it to execute the doTask
method on that delegate. Am I correct?
This is precisely what's happening.
In terms of your "where is the code behind doTask" question - it should be in whatever delegate registers itself with the class in question. (It's a bit confusing in that the same method name is being used but the intent behind the [self.delegate doTask]; line should be clear.)

Unnecessary warnings in the code

I am not able to find out why am I getting unnecessary warnings like:
"Method 'someMethod' not found"? Though at run time it is executing this method and I am getting the desired results. FYI... The called method resides in separate class which i have already imported in my class.
Usually one of two reasons:
1) You haven't casted the object that you're calling that method on correctly.
[(UITableView*)myTableView setDelegate:self];
2) The method that you're calling may not be in your custom Class' (public) #interface
#interface MyCustomClass : NSObject {
}
- (void)doSomethingReallyImportant;
#end
If you are trying to do something to an object, did you cast your object to the object's class?
If you are trying to access a method in your implementation of a class, do you have that method declared in your .h?
Then you have probably not put that method in the class' #interface. You should if it is a public method.
Being able to compile without warnings is a good thing.

Objective-C: call a method you just created

Simple question, as I am coming from another programming language. In Objective-C, lets say in a controller class I want to separate certain code into its own method, how do I call that method let's say, from viewLoad. As an example, let's say I create a method:
(void)checkIfInputCorrect
{
NSLog(#"text");
}
Now, i wanted to have in a delegate method, call this method. I tried [self checkIfInputCorrect] and get a warning saying Controller may not respond to -CheckIf...
I thought something like checkIfInputCorrect() would work that gives an error as well.
Basically how do you call a method?
Add this to your .h file
- (void)checkIfInputCorrect;
Call it with:
[self checkIfInputCorrect];
You need to list the method in the interface (ideal) or list the method implementation before the calling method (less ideal) so that the compiler can know that the class responds to the selector before it compiles the calling line.
To paraphrase Martin,
In your .m file, make sure your method -checkIfInputCorrect is placed so that it's physically above the method that has the line: [self checkIfInputCorrect];

How to use self class method on iPhone? (conceptual question)

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?
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
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.
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.
If you are getting a warning it might be because the method signature isn't in an interface.
#interface foo ....
-(void)method;
Once the implementation is written the warning should go away since it's not the first time the compiler has seen the method. It will work without doing this, but the warning message is annoying.