Objective-C: call a method you just created - iphone

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];

Related

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

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 }

invoking a delegate method of a class by code

is it possible to invoke a delegate method of a class by code?
In my case I want to invoke the searchBar:textDidChange: method in my code!
If yes how would I do that ?
Yes, if you know exactly how to cook the objects for its parameters.
It is just another method and you can call like
[self searchBar:parameter textDidChange:parameter];
IMHO, I would say, in this case, searchBar is in better position to pass the objects, rather than you invoke yourself.

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.

vs [mpk5 weaponAttachments]

I'm able to make the method for the call [self weaponAttachments:mpk5] but I don't like having to call self. I think [mpk5 weaponAttachments] is more natural and is easier to read.
The problem I'm having is I need to pass in the weapon (mpk5) in order to use it, which I can do with the first method but not with the second one. Does this mean that I need to subclass NSDictionary in order to be able to use a statement like [mpk5 weaponAttachments]? If so, how do I get ahold of the caller "mpk5" so that I can use it inside the method?
EDIT
I apologize for not putting this in the first time but my objective is to have [mpk5 weaponAttachments] return an NSDictionary or NSArray. Right now I have NSDictionary *attachments = [self weaponAttachments:mpk5]; which works but it just doesn't seem like the best approach.
So firstly, your two calls are a little mixed up:
[self weaponAttachments:mpk5] calls the weaponAttachments method, passing in the variable mpk5.
But [mpk5 weaponAttachments] is either asking the mpk5 object to return the weaponAttachments property or is asking the mpk5 object to run a method called weaponAttachments (I'm simplifying here - it's always a method, but if you're using properties you probably won't realise this as Objective-C will create them for you).
These are fundamentally different things.
On to the brunt of your question:
I don't like having to call self
...unfortunately, if you're working in an object-oriented language you're going to have to get used to this. Say I have a class called mySimpleClass, and a method inside that class called doSomething. Writing this:
[mySimpleClass doSomething] would be what we call a static method. Whereas calling [self doSomething] from within an instance of mySimpleClass would be an instance method.
If you're unsure of the difference between static and instance methods you should probably step back and take a look at some of the basic guides out there.

how to fault find if a delegate call is not being picked up in objective-c

Just implementing my first delegate in objective-C. I thought I had everything in place however the call from my AddController back to ListController isn't being picked up in the ListController.
Given that I'm not getting an exception, and that I can see that the code does get to the point in the AddController where it calls the delegate, are there any fault finding tips?
So for example:
given the "delegate" call (see below) did not throw an exception can I assume that my delegate declarations in the same file are OK?
"[delegate newItemController:self didFinishWithSave:YES];"
given the parent controller so to speak does have the delegate specified in the *.h definition (see below), then this does implied I've correctly implemented the method in the *.m file, noting I get no build errors?
#interface RootViewController : UITableViewController {
is there a known way for delegate calls to go missing without an exception if certain items don't like up (i.e. if there is what should I check for)
thanks
Most common error I've seen for a delegate method not being called is a nil delegate property. In other words, forgetting to specify who the delegate is?
As for debugging tips, anytime I've seen a problem where a delegate is not being called is to set breakpoints throughout the code and step through the code. Then you can see where things are going and what is or isn't being called.
Also, you mention exceptions a lot. Objective C prefers not to use exceptions as they are a relatively expensive call in the language (unlike say Java). Objective C can and does use exceptions but they are rare. You might want to "beaf up" your understanding of error handling in objective c.
Have you set your delegate variable like this in your AddController:
self.delegate = <instance of ListViewController>
If it has not been set, then the delegate would be nil and the method call to the nil would result in nothing. Otherwise, delegate calls wouldn't really go missing like that.
I didn't understand your second point though.