Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Program crashes - NSCFString objectForKeyedSubscript unrecognized selector sent to instance

When I run it, it crashes and gives this error:

NSCFString objectForKeyedSubscript unrecognized selector sent to instance.

I want to show markers on map from external JSON file, here below is my code:

    - (void)viewDidLoad
{
    [super viewDidLoad];
    [self.mapView setShowsUserLocation:YES];
    MKCoordinateRegion myRegion;
    CLLocationCoordinate2D center;
    center.longitude = 55.130761;
    center.latitude = 25.062718;
    //SPAN
    MKCoordinateSpan span;
    span.longitudeDelta = 0.40f;
    span.latitudeDelta = 0.40f;
    myRegion.span = span;
    myRegion.center = center;
    [self.mapView setRegion:myRegion animated:YES];
    //Loading Data from website
    NSURL *url = [NSURL URLWithString:@"http://example.com/api.php?user_id=2"];
    data = [NSData dataWithContentsOfURL:url];
    // parse the JSON into a NSArray
    NSError *error;
    NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
    if (error != nil)
    {
        ////
    }
    // a few variables to be used as we iterate through the array of results
    CLLocationCoordinate2D location;                         // coordinates of the annotation
    NSMutableArray *newAnnotations = [NSMutableArray array];
    MKPointAnnotation *newAnnotation;
    // iterate through the array, adding an annotation to our our array of new annotations
    for (NSDictionary *dictionary in array)
    {
        // retrieve latitude and longitude from the dictionary entry
        location.latitude = [dictionary[@"latitude"] doubleValue];
        location.longitude = [dictionary[@"longitude"] doubleValue];
        // create the annotation
        newAnnotation = [[MKPointAnnotation alloc] init];
        newAnnotation.title = dictionary[@"report_no"];
        newAnnotation.coordinate = location;
        [newAnnotations addObject:newAnnotation];
    }
    [self.mapView addAnnotations:newAnnotations];
}

Here is my JSON file how it looks like :

{
    reports: [
        {
            report_no: "3",
            user_id: "1",
            latitude: "25.085502",
            longitude: "55.158234"
        },
        {
            report_no: "4",
            user_id: "1",
            latitude: "24.931096",
            longitude: "55.018501"
        },
        {
            report_no: "6",
            user_id: "1",
            latitude: "24.993813",
            longitude: "55.094376",

        }
    ]
}

4 Answers

Stone Preston
Stone Preston
42,016 Points

try setting an exception breakpoint so you can find out which line is causing the error.

Thank you for your answer, I made what you've told me and here is the breakpoint :

location.latitude = [dictionary[@"latitude"] doubleValue];

But still I can not figure it out, any idea ?

Thank you for your answer, I made what you've told me and here is the breakpoint :

location.latitude = [dictionary[@"latitude"] doubleValue];

But still I can not figure it out, any idea ?

Stone Preston
Stone Preston
42,016 Points

you have to access the latitude using

location.coordinate.latitude 

not

location.latitude

But, when I replace it, it gives an error saying that : no member named 'coordinate' in CLLocationCoordinate2D

Stone Preston
Stone Preston
42,016 Points

sorry, i thought the location object was of type *CLLocation, not CLLocationCoordinate2d. My bad. hmm you are accessing the latitude correctly then. try using an NSLog on

[dictionary[@"latitude"] doubleValue]

and see what it outputs. There may be a problem with your dictionary if it doesnt output anything

I tried NSLog using the following :

 NSString *digit = [dictionary[@"latitude"] doubleValue];
    NSLog(@"%@", digit);

But it gives error saying that : Initializing 'NSString' *__strong' with an expression of incompatible type double.

Any help ?