An Objective-C Class

Many programming languages use the C language as the basis for their syntax. This can be helpful when switching between C, C++, and Java.

Although Objective-C draws from C as well, there are some language constructs that make it a bit odd to first understand.

To show what I'm talking about, I'll list a small class, and then describe the contents.

By convention, Objective-C classes have interface files (which end in .h) and implementation files (which end in .m):

FishingSpot.h

  1. #import <Foundation/Foundation.h>
  2.  
  3. @interface FishingSpot : NSObject
  4.  
  5. @property NSString *name;
  6. @property double latitude;
  7. @property double longitude;
  8. @property NSString *comments;
  9.  
  10. -(id) initWithName: (NSString *)_name atLat: (double)_latitude
  11. andLng: (double)_longitude;
  12.  
  13. -(id) init;
  14. @end

Notes:

  • Line 1: Make the class aware of some foundation classes. This is a standard import for all classes.
  • Line 3: The beginning of the interface section.
  • Lines 5-8: Declaration of properties. By default, Objective-C will provide getters and setters for you. There are ways to limit this, but that's out of scope for an introduction.
  • Lines 10-13: Two constructors. (Or initiators, as they're known in Objective-C.)
  • Line 14: The end of the interface section.

FishingSpot.m:

  1. #import "FishingSpot.h"
  2.  
  3. @implementation FishingSpot
  4.  
  5. @synthesize name, latitude, longitude, comments;
  6.  
  7. -(id) initWithName: (NSString *)_name atLat: (double)_latitude
  8. andLng: (double)_longitude {
  9.  
  10. self = [super init];
  11. if (self) {
  12. name = _name;
  13. latitude = _latitude;
  14. longitude = _longitude;
  15. comments = @"";
  16. }
  17. return self;
  18. }
  19.  
  20. -(id) init {
  21. return [self initWithName:@"undefined" atLat:0 andLng:0];
  22. }
  23.  
  24. -(NSString *)description {
  25. return [NSString stringWithFormat:@"%@ Latitude: %f Longitude: %f Timestamp: %@",
  26. name, latitude, longitude,
  27. ((comments == nil || comments.length < 1) ? @"none" : comments)];
  28. }
  29. @end

Notes:

  • Line 1: Import the header / interface file.
  • Line 3: The beginning of the implementation section.
  • Line 5: the @synthesize directive goes hand-in-hand with the @property directive. Behind the scenes getters and setters are generated.
  • Lines 7-18: The implementation of the designated initializer. Note that each parameter has a name as well as a variable. Also, the code is a common pattern in nearly all designated initializer. (Namely, call init on the superclass, and if it's successful, start updating local properties.
  • Lines 20-22: The implementation of another initializer. Notice that this code calls the designated initializer with default parameters. (This is also the convention for all non-designated initializers.
  • Lines 24-28: The implementation of how this class should be converted to a string. (This is the analog of Java's toString() method.)
  • Line 29: The end of the implementation section.