Converting CLLocationCoordinates2D into an NSValue

后端 未结 4 745
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-22 07:31

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:



        
相关标签:
4条回答
  • 2021-01-22 07:38

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

    _locationsArray = [[NSMutableArray alloc] init];
    
    0 讨论(0)
  • 2021-01-22 07:39

    Try:

    NSValue *locationValue = [NSValue valueWithBytes:&coordinates[i] objCType:@encode(CLLocationCoordinate2D)];
    
    0 讨论(0)
  • 2021-01-22 07:46

    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

    0 讨论(0)
  • 2021-01-22 07:51

    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);
    }
    
    0 讨论(0)
提交回复
热议问题