I’ve got the following code that works OK, but I’m not sure if I understood correctly some memory management concepts:
#import "mapPoint.h"
@implementation mapPoint
@synthesize coordinate, title, subtitle;
-(id)initWithCoordinate:(CLLocationCoordinate2D)c title:(NSString *)t {
[super init];
coordinate = c;
[self setTitle:t];
// Set date as subtitle
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSString *myDate = [dateFormatter stringFromDate:[NSDate date]];
[self setSubtitle:myDate];
[dateFormatter release];
// Look for city and state; when found, set it in subtitle, replacing date
geocoder = [[MKReverseGeocoder alloc] initWithCoordinate:c];
[geocoder setDelegate:self];
[geocoder start];
return self;
}
-(void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error {
NSLog(@"%@", error);
}
-(void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark {
[self setSubtitle:[NSString stringWithFormat:@"City: %@, State: %@", [placemark locality], [placemark administrativeArea]]];
}
-(void)dealloc {
[title release];
[subtitle release];
[geocoder release];
[super dealloc];
}
@end
- As I created the geocoder object via the
allocmethod, I have to release it (done indealloc). Correct? - In method
reverseGeocoder:didFindPlacemarka NSString is created with the convenience methodstringWithFormat. As I didn’t usealloc, I’m not responsible for releasing that (I assume that method usesautorelease). Is that right? - The object
placemarkreturns two NSStrings,localityandadministrativeArea. Again, I didn’t create those strings, so I’m not releasing them. However, given that they are part of the subtitle property of my mapPoint object, I don’t want those strings to disappear, but they probably were created withautorelease. The propertysubtitleis declared withretain. Is is correct to assume that it will retain the NSString created with the convenience method, preventing it’s premature destruction?
Thanks, and apologies if the questions are complicated… the subject is.
setSubtitle:yourself instead of allowing@synthesizeto do it, you would be responsible for implementing that behavior in your implementation ofsetSubtitle:.Edit:
I see in comments that you are particularly concerned about point 3. The strings from
[placemark locality]and[placemark administrativeArea]are passed as parameters to NSString’sstringWithFormatmethod, after which they are irrelevant because their content has been copied into the new string thatstringWithFormatreturns. That is the only string you really have to worry about, and as has been pointed outsetSubtitle:will retain that string.