0

我正在尝试在我的应用程序中实施地理围栏。我正在编写Kevin McMahon的精彩教程。

我的问题是每次尝试创建地理围栏时都会收到“EXC_BAD_ACCESS (code=1, address= ...)。

这是我的代码:

NSMutableArray *placeID = [[NSMutableArray alloc] init];
    NSMutableArray *lats = [[NSMutableArray alloc] init];
    NSMutableArray *longs = [[NSMutableArray alloc] init];

    for (int i = 0; i < [responseObject count]; i++) {
        [placeID addObject:[[responseObject objectAtIndex:i]objectForKey:@"id"]];
        [longs addObject:[[responseObject objectAtIndex:i]objectForKey:@"long"]];
        [lats addObject:[[responseObject objectAtIndex:i]objectForKey:@"lat"]];
    }

    NSMutableDictionary *latDict = [[NSMutableDictionary alloc] initWithObjects:lats forKeys:placeID];
    NSMutableDictionary *longDict = [[NSMutableDictionary alloc] initWithObjects:longs forKeys:placeID];

    NSMutableArray *geofences = [[NSMutableArray array] init];

    for(int k = 0; k < [responseObject count]; k++) {
        CLRegion *geofence = [self mapDictionaryToRegionWithLat:latDict withLong:longDict usingID:[placeID objectAtIndex:k]];
        [geofences addObject:geofence];
    }

    [self initializeRegionMonitoring:geofences];

创建 CLRegion

    - (CLRegion*)mapDictionaryToRegionWithLat:(NSDictionary *)latDict withLong:(NSDictionary *)longDict usingID: (NSString *)placeStringID {

        NSString *title = [NSString stringWithFormat:@"title %@", placeStringID];

        CLLocationDegrees latitude = [[latDict objectForKey:placeStringID] doubleValue];
        CLLocationDegrees longitude = [[longDict objectForKey:placeStringID] doubleValue];

        CLLocationCoordinate2D centerCoordinate = CLLocationCoordinate2DMake(latitude, longitude);

        CLLocationDistance regionRadius = 10.0;

        return [[CLRegion alloc] initCircularRegionWithCenter:centerCoordinate
                                                       radius:regionRadius
                                                   identifier:title];
    }

初始化区域监视器

    - (void) initializeRegionMonitoring:(NSArray*)geofences {
        if (locationManager == nil) {
            //[NSException raise:@"Location Manager Not Initialized" format:@"You must initialize location manager first."];
        }

        if(![CLLocationManager regionMonitoringAvailable]) {
            //[self showAlertWithMessage:@"This app requires region monitoring features which are unavailable on this device."];
            return;
        }

        for(CLRegion *geofence in geofences) { //ERROR HAPPENS HERE AFTER 7 ITERATION
            [locationManager startMonitoringForRegion:geofence];
            NSLog(@"geofence made here:\n\n%@\n\n",geofences);
        }
    }

该错误似乎发生在“for(CLRegion *geofence in geofences)”的第 7 次迭代(b/c 有 7 个地理围栏)之后,因为我在“locationManager:manage didStartMonitoringForRegion:region”中的 NSLog 没有出现。

4

1 回答 1

1

这个问题根本与地理围栏、位置或核心位置无关。我只是忘记释放我发起的这些可变数组:

NSMutableDictionary *latDict = [[NSMutableDictionary alloc] initWithObjects:lats forKeys:placeID];
NSMutableDictionary *longDict = [[NSMutableDictionary alloc] initWithObjects:longs forKeys:placeID];

刚刚在方法结束时发布了这些字典,一切正常!

于 2013-02-21T06:05:26.900 回答