0

我正在尝试通过从 JSON 数组中拉取字幕来为我的 MKAnnotation 添加字幕。我能够获得标题,并与我的视图控制器进行协调,如下所示,但我无法弄清楚如何才能从 JSON 键“cityName”中提取字幕。任何帮助都会很棒!谢谢!

MapViewController.m

location.latitude = [dictionary[@"placeLatitude"] doubleValue];
location.longitude = [dictionary[@"placeLongitude"] doubleValue];  

newAnnotation = [[MapViewAnnotation alloc] initWithTitle:dictionary[@"placeName"]
                                               andCoordinate:location];

MapViewAnnotation.h

@interface MapViewAnnotation : NSObject <MKAnnotation> {
NSString *title;
CLLocationCoordinate2D coordinate; 
}

@property (nonatomic, copy) NSString *title;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *subtitle;
- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d;
@end

MapViewAnnotation.m

#import "MapViewAnnotation.h"
@implementation MapViewAnnotation
@synthesize title, coordinate, subtitle;

- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d {
title = ttl;
coordinate = c2d;
subtitle = [SUBTITLE PULLED FROM JSON]
return self;
}
@end
4

2 回答 2

1

这里有什么问题?只需完全按照您对title房产所做的操作即可。

于 2013-02-12T08:14:06.670 回答
1

由于可能存在您没有初始化注释的每个属性的情况,因此我建议您不要将它们全部放入 init 方法中。

只需这样做,您就不需要更改 MapViewAnnotation.m 或 h

location.latitude = [dictionary[@"placeLatitude"] doubleValue];
location.longitude = [dictionary[@"placeLongitude"] doubleValue];  

newAnnotation = [[MapViewAnnotation alloc] initWithTitle:dictionary[@"placeName"]
                                           andCoordinate:location];
newAnnotation.subtitle = dictionary[@"cityName"];
于 2013-02-12T20:16:13.300 回答