Melvin
Melvin

Reputation: 3431

MapKit crashes - unrecognized selector sent to instance

I'm trying to add pin/address annotations in my Google maps view. When I do this everyting works fine:

[self addPin: CLLocationCoordinate2DMake(lat, lng) title: @"test" subtitle: @"test"];

with:

- (void) addPin: (CLLocationCoordinate2D) position title: (NSString *) pinTitle subtitle: (NSString *) pinSubtitle{

AddressAnnotation *addAnnotation = [[AddressAnnotation alloc] initWithTitle:pinTitle andCoordinate:position andSubtitle:pinSubtitle];

[mapView addAnnotation:addAnnotation];
[addAnnotation autorelease];

}

and

#import "AddressAnnotation.h"

@implementation AddressAnnotation

@synthesize title, coordinate, subtitle;

- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d andSubtitle:(NSString *)sbtitle {
[super init];
title = ttl;
coordinate = c2d;
subtitle = sbtitle;
return self;
}

- (void)dealloc {
[title release];
[super dealloc];
}

@end

with:

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

@interface AddressAnnotation : NSObject <MKAnnotation> {

NSString *title;
CLLocationCoordinate2D coordinate;
NSString *subtitle;
}

@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d andSubtitle:(NSString *)sbtitle;

@end

things go wrong when I do this:

NSString *detailTitle   = [NSString stringWithFormat:@"%@", [key objectForKey:@"name"]];
NSString *detailSubtitle    = [NSString stringWithFormat:@"%@", [key objectForKey:@"vicinity"]];

 NSLog(@"%@", detailSubtitle);
[self addPin: CLLocationCoordinate2DMake(lat, lng) title: detailTitle subtitle: detailSubtitle];

The app chrases somethimes when the pins are supposed to go in my view and somethimes this error:

'NSInvalidArgumentException', reason: '-[NSCFNumber length]: unrecognized selector sent to instance 0x5851c80'

Upvotes: 1

Views: 2114

Answers (2)

Rudy Velthuis
Rudy Velthuis

Reputation: 28806

NSCFNumber is one of the private implementations of NSNumber. NSNumber does not have a method called length, so it does not recognize the selector. So far so good.

It is hard to tell where this NSNumber is being used. ISTM that the problem is not in your code. I don't know enough of MapKit to give you a workaround.

Upvotes: 0

Mihai Fratu
Mihai Fratu

Reputation: 7663

Try changing your init function from AddressAnnotation.m to

- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d andSubtitle:(NSString *)sbtitle {
    self = [super init];
    if (self) {
        self.title = ttl;
        self.coordinate = c2d;
        self.subtitle = sbtitle;
    }
    return self;
}

You are using the instance variables directly to set the values in the class instance and not the properties so the actual values that you pass are not copied hence not retained.

Upvotes: 2

Related Questions