首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ClLocationCoordinate2D的NSMutableArray

ClLocationCoordinate2D的NSMutableArray
EN

Stack Overflow用户
提问于 2011-02-24 02:29:43
回答 5查看 18.5K关注 0票数 21

我试图创建并检索一个CLLocationCoordinate2D对象数组,但由于某些原因,该数组始终为空。

我有:

代码语言:javascript
复制
NSMutableArray *currentlyDisplayedTowers;
CLLocationCoordinate2D new_coordinate = { currentTowerLocation.latitude, currentTowerLocation.longitude };
[currentlyDisplayedTowers addObject:[NSData dataWithBytes:&new_coordinate length:sizeof(new_coordinate)] ];

我也尝试过这样添加数据:

代码语言:javascript
复制
[currentlyDisplayedTowers addObject:[NSValue value:&new_coordinate withObjCType:@encode(struct CLLocationCoordinate2D)] ];

无论哪种方式,currentlyDisplayedTowers计数总是返回零。你知道可能出了什么问题吗?

谢谢!

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2011-02-24 02:59:13

要停留在对象领域,您可以创建CLLocation的实例并将这些实例添加到可变数组中。

代码语言:javascript
复制
CLLocation *towerLocation = [[CLLocation alloc] initWithLatitude:lat longitude:lon];
[currentDisplayedTowers addObject:towerLocation];

要从CLLocation取回CLLocationCoordinate结构,请在对象上调用coordinate

代码语言:javascript
复制
CLLocationCoordinate2D coord = [[currentDisplayedTowers lastObject] coordinate];
票数 49
EN

Stack Overflow用户

发布于 2011-02-24 02:43:53

正如SB所说,确保您的数组已被分配和初始化。

您可能还希望像在第二个代码片段中那样使用NSValue包装。然后解码就像这样简单:

代码语言:javascript
复制
NSValue *wrappedCoordinates = [currentlyDisplayedTowers lastObject]; // or whatever object you wish to grab
CLLocationCoordinate2D coordinates;
[wrappedCoordinates getValue:&coordinates];
票数 3
EN

Stack Overflow用户

发布于 2011-02-24 02:33:55

你需要分配你的数组。

代码语言:javascript
复制
NSMutableArray* currentlyDisplayedTowers = [[NSMutableArray alloc] init];

然后你就可以使用它了。请确保在使用完release或使用其他工厂方法时调用release。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5095333

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档