我一直在创建一个应用程序,该应用程序应该在存储在 MySQL 数据库中的地图上显示 Pins。我使用 JavaScript 将数据库转换为 JSON,然后将 JSON 数据转换为 NSStrings。
这里的问题是我需要将其中两个 NSStrings 转换为双精度,因为我的 Pins 需要通过纬度和经度放置,下面是我的代码,当它构建时没有显示任何问题,但我的模拟器崩溃并显示了一些错误,我将直接在我的代码下发布。
危害.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface Hazards : NSObject <MKAnnotation>
@property (nonatomic, assign) CLLocationDegrees latitude;
@property (nonatomic, assign) CLLocationDegrees longitude;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@property (nonatomic, strong) NSString * ID;
@property (nonatomic, strong) NSString * ROUTE;
@property (nonatomic, strong) NSString * ADDRESS;
@property (nonatomic, strong) NSString * LATITUDE;
@property (nonatomic, strong) NSString * LONGITUDE;
@property (nonatomic, strong) NSString * HAZARD;
@property (nonatomic, strong) NSString * RISK;
// Methods
- (id) initWithID: (NSString *) hazardsID andROUTE: (NSString *) hazardsROUTE andADDRESS: (NSString *) hazardsADDRESS andLATITUDE: (NSString *) hazardsLATITUDE andLONGITUDE: (NSString *) hazardsLONGITUDE andHAZARD: (NSString *) hazardsHAZARD andRISK: (NSString *) hazardsRISK;
@end
危害.m
#import "Hazards.h"
@implementation Hazards
@synthesize coordinate, title, subtitle, ID, ROUTE, ADDRESS, LATITUDE, LONGITUDE, HAZARD, RISK, latitude, longitude;
- (id) initWithID: (NSString *) hazardsID andROUTE: (NSString *) hazardsROUTE andADDRESS: (NSString *) hazardsADDRESS andLATITUDE: (NSString *) hazardsLATITUDE andLONGITUDE: (NSString *) hazardsLONGITUDE andHAZARD: (NSString *) hazardsHAZARD andRISK: (NSString *) hazardsRISK {
self = [super init];
if (self)
{
ID = hazardsID;
ROUTE = hazardsROUTE;
ADDRESS = hazardsADDRESS;
LATITUDE = hazardsLATITUDE;
LONGITUDE = hazardsLONGITUDE;
HAZARD = hazardsHAZARD;
RISK = hazardsRISK;
latitude = [hazardsLATITUDE doubleValue]; // convert the string hazardsLatitude to a double here
longitude = [hazardsLONGITUDE doubleValue]; // convert the string hazardsLongitude to a double here
}
return self;
}
-(NSString *)title
{
return self.HAZARD;
}
-(NSString *)subtitle
{
return self.ADDRESS;
}
@end
视图控制器.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import "Hazards.h"
@interface ViewController : UIViewController
@property (nonatomic, strong) NSMutableArray *json;
@property (nonatomic, strong) NSMutableArray *hazardsArray;
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
#pragma mark - Methods
-(void) retrieveData;
@end
视图控制器.m
#import "ViewController.h"
#import "Hazards.h"
@interface ViewController ()
@end
// Railway Street Ballymena Coordinates
#define BALLYMENA_LATITUDE 54.857719;
#define BALLYMENA_LONGITUDE -6.280654;
// Span
#define THE_SPAN 0.01f;
#define getDataURL @"www.ryanball.net/royalmail/json.php"
@implementation ViewController
@synthesize json, hazardsArray, mapView;
- (void)viewDidLoad
{
[super viewDidLoad];
// Create the region
MKCoordinateRegion myRegion;
// Center
CLLocationCoordinate2D center;
center.latitude = BALLYMENA_LATITUDE;
center.longitude = BALLYMENA_LONGITUDE;
//Span
MKCoordinateSpan span;
span.latitudeDelta = THE_SPAN;
span.longitudeDelta = THE_SPAN;
myRegion.center = center;
myRegion.span = span;
// Set our mapview
[mapView setRegion:myRegion animated: YES];
// Annotation
NSMutableArray *locations = [[NSMutableArray alloc] init];
CLLocationCoordinate2D location;
Hazards *myAnn;
// Pin to show Royal Mail Ballymena delivery office
myAnn = [[Hazards alloc] init];
location.latitude = BALLYMENA_LATITUDE;
location.longitude = BALLYMENA_LONGITUDE;
myAnn.coordinate = location;
myAnn.title = @"Royal Mail Ballymena";
myAnn.subtitle = @"111, Railway Street";
[locations addObject:myAnn];
[self.mapView addAnnotations:locations];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Methods
-(void) retrieveData {
NSURL *url = [NSURL URLWithString:getDataURL];
NSData *data = [NSData dataWithContentsOfURL:url];
json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
// Setup our hazards array
hazardsArray = [[NSMutableArray alloc] init];
for (int i =0; i < json.count; i++) {
// Create hazard object
NSString *hazardsID = [[json objectAtIndex:i] objectForKey:@"ID"];
NSString *hazardsROUTE = [[json objectAtIndex:i] objectForKey:@"ROUTE"];
NSString *hazardsADDRESS = [[json objectAtIndex:i] objectForKey:@"ADDRESS"];
NSString *hazardsLATITUDE = [[json objectAtIndex:i] objectForKey:@"LATITUDE"];
NSString *hazardsLONGITUDE = [[json objectAtIndex:i] objectForKey:@"LONGITUDE"];
NSString *hazardsHAZARD = [[json objectAtIndex:i] objectForKey:@"HAZARD"];
NSString *hazardsRISK = [[json objectAtIndex:i] objectForKey:@"RISK"];
Hazards *myHazards = [[Hazards alloc] initWithID:hazardsID andROUTE:hazardsROUTE andADDRESS:hazardsADDRESS andLATITUDE:hazardsLATITUDE andLONGITUDE:hazardsLONGITUDE andHAZARD:hazardsHAZARD andRISK:hazardsRISK];
// Add our hazards object to our hazards array
[hazardsArray addObject:myHazards];
}
[self.mapView addAnnotations:hazardsArray];
}
@end
这是我在控制台中得到的错误日志:
2013-04-25 09:04:43.628 RoyalMailApp[6596:c07] -[Hazards setCoordinate:]: unrecognized selector sent to instance 0x8c63a60
2013-04-25 09:04:43.629 RoyalMailApp[6596:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Hazards setCoordinate:]: unrecognized selector sent to instance 0x8c63a60'
*** First throw call stack:
(0x1b3c012 0x1458e7e 0x1bc74bd 0x146c7ea 0x1b2bcf9 0x1b2b94e 0x2698 0x47e1c7 0x47e232 0x3cd3d5 0x3cd76f 0x3cd905 0x3d6917 0x39a96c 0x39b94b 0x3accb5 0x3adbeb 0x39f698 0x1a97df9 0x1a97ad0 0x1ab1bf5 0x1ab1962 0x1ae2bb6 0x1ae1f44 0x1ae1e1b 0x39b17a 0x39cffc 0x215d 0x2085 0x1)
libc++abi.dylib: terminate called throwing an exception
(lldb)