gpt4 book ai didi

objective-c - 为什么我不能在前一个 View Controller 上查看结构值

转载 作者:行者123 更新时间:2023-12-01 18:33:03 24 4
gpt4 key购买 nike

我对Objective-C(和C本身)很陌生,所以这可能真的很容易,但我有点卡住了。

我有一个需要用户输入的 View Controller ,我有一个表格 View ,其中有一行表示纬度和经度值。当用户点击该行时,它会将他们带到一个带有 map View 的新 View Controller 。

我有一个标记供他们拖动并选择他们的位置:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)annotationView 
didChangeDragState:(MKAnnotationViewDragState)newState fromOldState:(MKAnnotationViewDragState)oldState

在这种方法中,我想用新坐标更新以前的 View Controller 。

它有一个属性:
CLLocationCoordinate2D destinationCoordinates;

我已将其设置为非原子、分配和合成

我的代码是这样做的:
NSArray *allControllers = self.navigationController.viewControllers;

EnterDetailsViewController *parent = [allControllers objectAtIndex:([allControllers count] -2)];

if ([parent isKindOfClass:[EnterDetailsViewController class]])
{
CLLocationCoordinate2D theCoordinate;
theCoordinate.latitude = annotation.coordinate.latitude;
theCoordinate.longitude = annotation.coordinate.longitude;

parent.destinationCoordinates = theCoordinate;

NSLog(@"Set coord to %@ - %@",
parent.destinationCoordinates.latitude,
parent.destinationCoordinates.latitude);

但是 NSLog总是以 EXC_BAD_ACCESS 崩溃- 为什么是这样?我在这里误解了什么核心概念?

最佳答案

CLLocationCoordinate2D的定义是:

typedef struct {
CLLocationDegrees latitude;
CLLocationDegrees longitude;
} CLLocationCoordinate2D;

您使用 %@ 打印对象,请尝试:
NSLog(@"Set coord to %f - %f",
parent.destinationCoordinates.latitude,
parent.destinationCoordinates.longitude);

请注意 CLLocationDegrees是 double 型:
typedef double CLLocationDegrees;

关于objective-c - 为什么我不能在前一个 View Controller 上查看结构值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6031233/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com