Newbie questions on the MapPoint implementation


#1

- (id) initWithCoordinate: (CLLocationCoordinate2D) c title:(NSString *) t { [super init]; coordinate = c; [self setTitle: t]; return self; }

2 newbie questions:

  1. Why super? If we are making MapPoint to store the data (because MKAnnotation can’t), and this is a method of MapPoint, why isn’t it [self init]? Who is super? NSObject?

  2. How would I know that the setTitle method existed? We didn’t define it. I don’t see a setTitle instance or class method defined in the Apple documentation for NSObject, MKAnnotation, etc. Can I assume there is a setX method for every class variable (i.e. the @synthesize title created the setTitle: setter)? Is this the same as self.title = *t;?

Thanks!


#2

Hi,

  1. Yes in this case super is NSObject. In your mapPoint.h you have @interface MapPoint : NSObject stating that MapPoint is a subclass of NSObject.
    You need to call [super init] as there will be some initialisation code in NSObject (that you can’t see) that will probably perform some memory initialisation and set things up for the runtime so that your mapPoint object will behave as a true Objective-C object.

If mapPoint was a subclass of say mySuperMapPoint then you would still call [super init] (or whatever the designated initialiser for mySuperMapPoint is) which in turn should call [super init] so that ultimately a call to NSObject’s init is performed.

Nearly all classes ultimately inherit from NSObject (there are a handful of specialised exceptions).

  1. @synthesize title” created the setTitle method for you using the information you supplied in the @property statement. If you left out the @synthesize statement you would have to write your own implementation of setTitle to be able to use [self setTitle].
    [self setTitle: t] is the same as self.title = t although many people feel that the former is better practice as you can easily see that it is invoking a method.

HTH
Gareth