Why are methods not declared in .h file?


#1

I’m pulling my hair over this from past few days…

In Whereamiviewcontroller.m file, we have implemented several methods such as:

[code]
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
NSLog(@"%@",locations);
}

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@“Could not find location: %@”,error);
}

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{

CLLocationCoordinate2D loc = [userLocation coordinate];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 250, 250);
[worldView setRegion:region animated:YES];

}[/code]

But we have not declared any of these methods in .h file…and still the program runs fine??? How?!

Also, why are we not using @property to declare instance variables in .h file?


#2

You don’t really have to declare methods in Objective-C.

As long as the implementation of a method exists, the runtime system will find and execute it. However, declaring them helps humans to comprehend the interface of a class and enables Xcode to perform error checking. You should read about the method selectors to learn how all this works.

Objective-C is the most magical programming language in the entire solar system :slight_smile:


#3

Is it so? Okay! :unamused:

And what about using @property and @synthesize for instance variables? Why haven’t the author used @property for instance variables in this program?


#4

Instance variables and properties are not the same things, but they are related; properties are interfaces to instance variables. Accessing an instance variable directly, whenever you can, incurs no overhead as opposed to accessing it through a property.


#5

I do believe because the methods you are invoking are actually declared in a class that is higher up in the inheritance chain. You DO have to declare methods in the .h files. if you don’t you will get a compile error stating that "No visible @interface for ‘classname’ declares the selector ‘methodname’

Try it out:

Create a test empty project. Create an Objective-C class and write a -(void) method in .m but don’t declare it in .h
in the app delegate create an instance pointer to the class and try to call that method.

-(void)locationManager:(CLLocationManager *)manager… methods come from the CLLocationManagerDelegate protocol
(apple declared the method in the delegate protocol class already and since you implemented it you don’t re declare it but you override or implement it

same thing with mapView methods, they are part of the MKMapViewDelegate protocol. You will override existing methods more than writing your own in a lot of cases.


#6

These methods are defined in the protocols you’re implementing - they’re declared when you tell the compiler you’re implementing those protocols: <CLLocationManagerDelegate, MKMapViewDelegate, UITextFieldDelegate>.

Also, you only have to declare a method if it’s called before the implementation.

This is Objective C showing it’s underlying roots in C - all our “magical” methods are really just functions. If the function is defined somewhere before it’s called, the compiler shouldn’t complain.

You just have to get used to the fact that Objective C’s object orientedness is poorly-enforced. (Put a positive spin on it: it’s “flexible”). Nothing is really private, encapsulated or abstract, and you can get away with all kinds of non-textbook programming if you want. Categories are especially hax :slight_smile:

Heck, you can program in C. Don’t worry about the style so much - go with what works for you. The language is crazy flexible.


#7

[quote=“ibex10”][u][color=#000000]You don’t really have to declare methods in Objective-C.

As long as the implementation of a method exists, the runtime system will find and execute it. However, declaring them helps humans to comprehend the interface of a class and enables Xcode to perform error checking. You should read about the method selectors to learn how all this works.

Objective-C is the most magical programming language in the entire[/color] [color=#000000]solar panel[/color][color=#000000] :slight_smile:[/color][/u][/quote]
Yes I do love programming in Objective-C… It is nice basic language to learn command on other languages.