我试图创建并检索一个CLLocationCoordinate2D对象数组,但由于某些原因,该数组始终为空。
我有:
NSMutableArray *currentlyDisplayedTowers;
CLLocationCoordinate2D new_coordinate = { currentTowerLocation.latitude, currentTowerLocation.longitude };
[currentlyDisplayedTowers addObject:[NSData dataWithBytes:&new_coordinate length:sizeof(new_coordinate)] ];我也尝试过这样添加数据:
[currentlyDisplayedTowers addObject:[NSValue value:&new_coordinate withObjCType:@encode(struct CLLocationCoordinate2D)] ];无论哪种方式,currentlyDisplayedTowers计数总是返回零。你知道可能出了什么问题吗?
谢谢!
发布于 2011-02-24 02:59:13
要停留在对象领域,您可以创建CLLocation的实例并将这些实例添加到可变数组中。
CLLocation *towerLocation = [[CLLocation alloc] initWithLatitude:lat longitude:lon];
[currentDisplayedTowers addObject:towerLocation];要从CLLocation取回CLLocationCoordinate结构,请在对象上调用coordinate。
CLLocationCoordinate2D coord = [[currentDisplayedTowers lastObject] coordinate];发布于 2011-02-24 02:43:53
正如SB所说,确保您的数组已被分配和初始化。
您可能还希望像在第二个代码片段中那样使用NSValue包装。然后解码就像这样简单:
NSValue *wrappedCoordinates = [currentlyDisplayedTowers lastObject]; // or whatever object you wish to grab
CLLocationCoordinate2D coordinates;
[wrappedCoordinates getValue:&coordinates];发布于 2011-02-24 02:33:55
你需要分配你的数组。
NSMutableArray* currentlyDisplayedTowers = [[NSMutableArray alloc] init];然后你就可以使用它了。请确保在使用完release或使用其他工厂方法时调用release。
https://stackoverflow.com/questions/5095333
复制相似问题