Luc*_*uca 2 objective-c mkannotation ios
注释在地图上显示正常,但是当我点击一个时,我会引发此异常:
[NSNull length]: unrecognized selector sent to instance
'NSInvalidArgumentException', reason: '-[NSNull length]: unrecognized selector sent to instance
Run Code Online (Sandbox Code Playgroud)
我的相关代码是这样的:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
static NSString *identifier = @"MyLocation";
if ([annotation isKindOfClass:[MyLocation class]]) {
MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [mapView2 dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
} else {
annotationView.annotation = annotation;
}
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
annotationView.pinColor = MKPinAnnotationColorRed;
return annotationView;
}
return nil;
}
Run Code Online (Sandbox Code Playgroud)
并配置注释:
NSNumber * latitude = [[row objectAtIndex:21]objectAtIndex:1];
NSNumber * longitude = [[row objectAtIndex:21]objectAtIndex:2];
NSString * crimeDescription = [row objectAtIndex:17];
NSString * address = [row objectAtIndex:13];
CLLocationCoordinate2D coordinate;
coordinate.latitude = latitude.doubleValue;
coordinate.longitude = longitude.doubleValue;
MyLocation *annotation = [[MyLocation alloc] initWithName:crimeDescription address:address coordinate:coordinate] ;
[mapView2 addAnnotation:annotation];
Run Code Online (Sandbox Code Playgroud)
并且在MyLocation持有注释的类中:
- (id)initWithName:(NSString*)name address:(NSString*)address coordinate:(CLLocationCoordinate2D)coordinate {
if ((self = [super init])) {
_name = [name copy];
_address = [address copy];
_coordinate = coordinate;
}
return self;
}
Run Code Online (Sandbox Code Playgroud)
小智 6
当您点击注释视图时,它会尝试获取length注释的内容,title以便它知道调出的宽度.
您获得的异常表示该title注释的某种方式设置为NSNull对象而不是NSString.
一个NSNull对象不具有length这样你会得到"无法识别的选择"异常的方法.
您还没有展示如何title在MyLocation类中实现,但我假设它在创建注释时返回_name您正在设置的crimeDescription(来自row数组).
不知何故,row数组中的值有一个NSNull而不是一个NSString.
如果你不需要的NSNull值,你可以更改设置代码row数组值,因此不会把NSNullS(它可以把NSString好像@"Unknown"代替).
另一种选择是,你可以修改getter方法为title在MyLocation类,并将它返回一个"未知" NSString,如果_name是的NSNull.
例如:
-(NSString *)title
{
if ([_name isKindOfClass:[NSNull class]])
return @"Unknown";
else
return _name;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2809 次 |
| 最近记录: |