我正在尝试在 map 上显示存储在NSArray中的对象。在执行了一种获取距离用户2英里范围内的位置的方法之后,将从Parse.com类(关联公司)中提取对象。我已使用NSLog确认查询是否正确执行,因此我知道提取操作也正确执行。

当我尝试运行该应用程序时,我在for(NSDictionary)部分抛出了一个错误,该应用程序冻结了。错误状态为“线程1:EXC_BAD_ACCESS(访问代码1,地址0x80120)”与Google协商后,我发现这是某种类型的内存分配问题,但我不知道为什么会发生或如何解决。

#import "ViewController.h"
#import "MapAnnotation.h"

@import CoreLocation;

@interface ViewController () <CLLocationManagerDelegate>

@property (nonatomic,strong) NSArray *affiliates;

@end

@implementation ViewController

- (void)viewDidLoad {


    [super viewDidLoad];

    // Ask for authorization to collect location from user
    CLLocationManager * locationManager = [[CLLocationManager alloc] init];
    // Check for iOS 8. Without this guard the code will crash with "unknown selector" on iOS 7.
    if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {

        locationManager.delegate = self;
        locationManager.distanceFilter = kCLDistanceFilterNone;
        locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        [locationManager startUpdatingLocation];
        [locationManager requestWhenInUseAuthorization];
    }

    //Set map options
    self.mapView.showsUserLocation = YES;
    self.mapView.delegate = self;
    self.mapView.scrollEnabled = YES;
    self.mapView.zoomEnabled = YES;
    self.mapView.userTrackingMode = YES;

    // Store the user's location as a Parse PFGeoPoint and call the fetchAffiliatesNearPoint method
    [PFGeoPoint geoPointForCurrentLocationInBackground:^(PFGeoPoint *geoPoint, NSError *error) {
        if (!error) {
            [self fetchAffiliatesNearPoint:geoPoint];
            NSLog(@"Got User Location! %@", geoPoint);
        }
    }];

    /* This is where I am having issues

    for(NSDictionary *affiliates in affiliates) {
        CLLocationCoordinate2D annotationCoordinate = CLLocationCoordinate2DMake([affiliates[@"latitude"] doubleValue], [affiliates[@"longitude"] doubleValue]);

        MapAnnotation *annotation = [[MapAnnotation alloc] init];
        annotation.coordinate = annotationCoordinate;
        annotation.title = affiliates[@"name"];
        annotation.subtitle = affiliates[@"url"];
        [self.mapView addAnnotation:annotation];
    }

*/

}


//Fetch an array of affiliates that are within two miles of the user's current location.
- (void)fetchAffiliatesNearPoint:(PFGeoPoint *)geoPoint
{

    PFQuery *query = [PFQuery queryWithClassName:@"Affiliates"];
    [query whereKey:@"geometry" nearGeoPoint:geoPoint withinMiles:2.0];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error)
        {
            self.affiliates = objects;
            NSLog(@"Nearby Locations %@", _affiliates);
        }
    }];
}


-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{


    //Zoom map to users current location
    if (!self.initialLocation) {
        self.initialLocation = userLocation.location;
        MKCoordinateRegion mapRegion;
        mapRegion.center = mapView.userLocation.coordinate;
        mapRegion.span.latitudeDelta = .025;
        mapRegion.span.longitudeDelta = .025;

        [mapView setRegion:mapRegion animated: YES];
        }

}
@end

最佳答案

我也没有使用过Parse,但是有几个明显的问题可能会有所帮助:

  • 这行没有意义:
    for(NSDictionary *affiliates in affiliates)
    

    在此,affiliates之后的in指的是本地声明的字典变量,而不是相同名称的property变量。由于局部变量没有初始化的引用,并且指向一些随机内存,因此可以得到EXC_BAD_ACCESS。使用self.affiliates显式引用该属性。

    另外,将循环变量命名为与其循环通过的数组相同的名称非常令人困惑。您正在遍历一组“附属”(复数)。该数组包含字典,每个字典都是一个“从属”(单数)。将字典变量命名为affiliate(单数)会减少混乱。例:
    for (NSDictionary *affiliate in self.affiliates)
    

    还将循环内的其余引用从affiliates更改为affiliate
  • 当前,self.affiliates启动后立即完成geoPointForCurrentLocationInBackground的循环。 geoPointForCurrentLocationInBackground似乎是异步的,这意味着self.affiliates上的循环将在geoPointForCurrentLocationInBackground实际设置该属性之前执行。

    不要在刚启动self.affiliates之后立即遍历viewDidLoad中的geoPointForCurrentLocationInBackground,而是将循环移到findObjectsInBackgroundWithBlock的完成块内部(在设置self.affiliates之后)。例:
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error)
        {
            self.affiliates = objects;
            NSLog(@"Nearby Locations %@", _affiliates);
    
            for(NSDictionary *affiliate in self.affiliates) {
                ...
            }
        }
    }];
    
  • 关于ios - 将解析的PFGeoPoints数组显示为 map 注释错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28788975/

    10-13 06:34