Converting CLLocationCoordinates2D into an NSValue

こ雲淡風輕ζ 提交于 2019-12-20 03:14:56

问题


I'm simply trying to convert CLLocationCoordinates into an NSValue to be used in an array brought back later in my app. Here is my code:

  NSUInteger count = [self.locations count];
    CLLocationCoordinate2D coordinates[count];
    for (NSInteger i = 0; i < count; i++) {
        coordinates[i] = [(CLLocation *)self.locations[i] coordinate];
        NSValue *locationValue = [NSValue valueWithMKCoordinate:coordinates[i]];
        [_locationsArray addObject:locationValue];
        NSLog(@"location = %@", _locationsArray);
    }

However the NSLog displays that the array is filled with (Null) values. I'm not sure where I have gone wrong here, an someone point me in the right direction?


回答1:


I didn't see any need in coordinatesarray. And you probably didn't initialised it. Try:

_locationsArray = [NSMutableArray array];
for (CLLocation *location in self.locations) {
    NSValue *locationValue = [NSValue valueWithMKCoordinate: location.coordinate];
    [_locationsArray addObject:locationValue];
    NSLog(@"location = %@", _locationsArray);
}



回答2:


Hi you can convert the CLLocationCoordinates2D into an NSValue by doing like below but have you check the coordinate contains any value or not once.

NSValue *value = [NSValue valueWithMKCoordinate:coordinate];

Thanks




回答3:


Try:

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



回答4:


It can be that you didn't initialize your array. That's why it's nil when you log it.

_locationsArray = [[NSMutableArray alloc] init];


来源:https://stackoverflow.com/questions/22113675/converting-cllocationcoordinates2d-into-an-nsvalue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!