Calling coordinates from MySQL database with this code causes simulator to display a SIGABRT error. What's wrong?

杀马特。学长 韩版系。学妹 提交于 2019-12-25 07:59:12

问题


When I use this code to try and call Lat & Long coordinates from a MySQL database (and display them on a MapView), the app runs fine. Everything appears to work, but for some reason, xcode pauses the simulator before the map loads, and throws me a SIGABRT. Any idea why? See below:

MapViewController.h

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


    @interface MapViewController : UIViewController  <MKMapViewDelegate> 

        @property (nonatomic, strong) IBOutlet MKMapView *mapView;
        @property (nonatomic, retain) NSMutableArray *dispensaries;
        @property (nonatomic, retain) NSMutableData *data;


    @end

MapViewController.m

#import "MapViewController.h"
#import "MapViewAnnotation.h"


    @implementation MapViewController
    @synthesize mapView;
    @synthesize dispensaries;
    @synthesize data;



    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }

    - (void)didReceiveMemoryWarning
    {
        // Releases the view if it doesn't have a superview.
        [super didReceiveMemoryWarning];

        // Release any cached data, images, etc that aren't in use.
    }

    #pragma mark - View lifecycle

    - (void)viewDidLoad {

    [super viewDidUnload];



            NSLog(@"Getting Device Locations");

            NSString *hostStr = @"http://stylerepublicmagazine.com/dispensaries.php";
            NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:hostStr]];
            NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding];
            NSLog(@"server output: %@", serverOutput);
            NSMutableArray *array = (NSMutableArray *)dispensaries;
            dispensaries = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];

            for (NSDictionary *dictionary in array) {



                CLLocationCoordinate2D coord = {[[dictionary objectForKey:@"lat"] doubleValue], [[dictionary objectForKey:@"lng"] doubleValue]};

                MapViewAnnotation *ann = [[MapViewAnnotation alloc] init];
                ann.title = [dictionary objectForKey:@"Name"];
                ann.coordinate = coord;
                [mapView addAnnotation:ann];



        [mapView setMapType:MKMapTypeStandard];
        [mapView setZoomEnabled:YES];
        [mapView setScrollEnabled:YES];



        self.mapView.delegate = self;


    }

    }

    - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation

    {
        MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 800, 800);
        [self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];


    MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
    point.coordinate = userLocation.coordinate;
    point.title = @"You Are Here";
    point.subtitle = @"Your current location";

    [self.mapView addAnnotation:point];



    }

回答1:


Ok, so now you have the JSON. You are going to have to decode that using NSXMLParser. Or, if you want to turn that into objects, you could use my open source library on GitHub, which allows you to simply implement NSCoding protocol and instantiate objects (or encode them) as JSON. That's here.



来源:https://stackoverflow.com/questions/14543777/calling-coordinates-from-mysql-database-with-this-code-causes-simulator-to-displ

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!