2012-03-26 89 views
0

新的使用NSDictionary,但需要一些指针,我做错了什么。转换NSDictionary initWithObjectAndKeys

我有一个NSDictionary,存储浮点值坐标:

页眉:

@class DetailViewController; 
@interface MasterViewController : UITableViewController 
@property (strong, nonatomic) DetailViewController *detailViewController; 
@property (strong, nonatomic) NSArray *DestinationData; 
@property (strong, nonatomic) NSArray *DestinationSections; 

- (void)createDestinations; 

主营:

@implementation MasterViewController 

@synthesize detailViewController = _detailViewController; 
@synthesize DestinationData; 
@synthesize DestinationSections; 

NSMutableArray *museumArray; 

self.DestinationSections=[[NSArray alloc]initWithObjects: 
          ... 
          ... 
          (@"Museums"), nil]; 

[museumsArray addObject:[[NSDictionary alloc] 
        initWithObjectsAndKeys:@"Museum",@"Name", 
        @"51.5189100",@"Dest Lat", 
        @"-0.1263840",@"Dest Long", 
        nil]]; 

self.DestinationData=[[NSArray alloc] initWithObjects: 
museumsArray, nil]; 

我希望能够从字典返回值并使用它与CLLocationCoordinate2D:

在DetailViewController中。 m:

CLLocationCoordinate2D destinationLocation; 

如何设置该值,假设它应该是双倍的?

destinationLocation.latitude = [[self.detailItem objectForKey:@"Dest Lat"] doubleValue]; 
destinationLocation.longitude = [[self.detailItem objectForKey:@"Dest Long"] doubleValue]; 

MapViewAnnotation *destAnnotation = [[MapViewAnnotation alloc] initWithTitle:@"British Museum" andCoordinate:destinationLocation]; 
[self.detailGoogleMap addAnnotation:destAnnotation]; 
+0

您收到了什么错误? – 2012-03-26 12:33:48

+0

咦?你有什么问题?? – yuji 2012-03-26 12:34:38

+0

当我通过我的坐标来绘制地图注记时,它不会将注记绘制在正确的位置。 – Jeremy 2012-03-26 12:48:02

回答

1

如果你只存储坐标串,因为你不知道如何存储CLLocation2D,有一个更好的解决方案:使用NSValue包的位置。

NSValue *locationValue = [NSValue valueWithBytes:&destinationLocation 
             objCType:@encode(CLLocationCoordinate2D)]; 

locationValue现在是可以在像字典或阵列的容器储存善意Objective-C的对象。稍后,当您想要检索位置坐标时,请执行以下操作:

CLLocationCoordinates2D destination; 
NSValue *locationValue = [myDictionary objectForKey:@"destination"]; 
[location getValue:&destination]; 
+0

它看起来像一个类在引用另一个类之前有一个问题,在设置值之前。迦勒 - 感谢你。我会试一试。 – Jeremy 2012-03-26 17:10:04

相关问题