Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MACloop

macrumors 6502
Original poster
May 18, 2009
393
0
Germany
Hi,
in order to use OO design when programming, I use some custom-objects. Those objects inherits from NSObject. For instance, I have a class House and a class MapAnnotation. House is only a NSObject and the MapAnnotations class inherits from MKAnnotation.

The first question:
do I always need to define an init-method for custom-objects? I didn't do that now and it seems to work, but the question is, which solution isthe best one?

And a (I think) memory management question:
If the House object has a MapAnnotation defined and this is going to be displayed on a map, how do I do this in a proper way. The solutions I have tried is leading to a crash or a leak.
My intention is that when a House object is created, the annotation for this object is created automatically. All the data available in the House object may be used to define an annotation-object.

I thought I should do something like this:

House.h-class
Code:
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#import "MapAnnotation.h"

@interface House : NSObject {
	NSString *title;
	double longitude;
	double latitude;
	CLLocationCoordinate2D *coordinate;
	MapAnnotation *annotation;
}

@property(nonatomic, retain) NSString *title;
@property(nonatomic, assign) double longitude;
@property(nonatomic, assign) double latitude;
@property(nonatomic, assign) CLLocationCoordinate2D *coordinate;
@property(nonatomic, retain) MapAnnotation *annotation;
@end

House.m-class
Code:
#import "House.h"
@implementation House
@synthesize title,longitude,latitude,coordinate,annotation;

- (CLLocationCoordinate2D)coordinate{
    coordinate.latitude = self.latitude;
    coordinate.longitude = self.longitude;
    return coordinate;
}

- (MapAnnotation)annotation{
    annotation.latitude = self.latitude;
    annotation.longitude = self.longitude;
    annotation.title = self.title;
    return annotation;
}

- (void) dealloc {
	[longitude release];
	[latitude release];
	[annotation release];
	[title release];
	[super dealloc];
}
@end


Code:
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface MapAnnotation : NSObject<MKAnnotation>{
	CLLocationCoordinate2D coordinate;
	NSString *title;
	NSString *subtitle;
	NSNumber *latitude;
	NSNumber *longitude;
	NSString *annotationID;
}

@property (nonatomic, retain) NSString *title;
@property (nonatomic, retain) NSString *subtitle;
@property (nonatomic, retain) NSNumber *latitude;
@property (nonatomic, retain) NSNumber *longitude;
@property (nonatomic, retain) NSString *annotationID;
@property (nonatomic, assign, readonly) CLLocationCoordinate2D coordinate;

@end

Code:
#import "MapAnnotation.h"
@implementation MapAnnotation
@synthesize longitude,latitude, coordinate, title, annotationID, subtitle;

+ (NSSet *)keyPathsForValuesAffectingCoordinate{
    return [NSSet setWithObjects:@"latitude", @"longitude", nil];
}

- (CLLocationCoordinate2D)coordinate{
    coordinate.latitude = self.latitude.doubleValue;
    coordinate.longitude = self.longitude.doubleValue;
    return coordinate;
}

- (void)dealloc {
	[longitude release];
	[latitude release];
	[title release];
	[subtitle release];
	[annotationID release];
	[super dealloc];
}

@end

Is this the correct way to define and/or use custom objects?
Thanks in advance!
MACloop
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.