如何从一个方法返回多个值
我有 -(CLLocationCooperative2D) 方法,我想从该方法返回多个位置(这些位置在另一个方法中使用) 我的方法看起来像这样,
-(CLLocationCoordinate2D) addressLocation{
- --------
----------
return location;
}
是否可以返回多个位置(我的意思是 return location1 ,return location2 . ..)?请帮我 谢谢
I have -(CLLocationCoordinate2D) method, I want to return multiple locations from that method (those locations are using in another method)
my method looks like this,
-(CLLocationCoordinate2D) addressLocation{
- --------
----------
return location;
}
is it possible to return multiple locations (I mean return location1 , return location2 . . ..) ?? please help me
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
CLLocationCooperative2D 不是一个对象,因此不能将它们添加到数组中并返回。因为它们是结构体,所以有几个选项,
malloc
以旧方式分配一个数组,并将结构数据复制到数组malloc
一个新的结构体指针,复制数据,然后将指针存储在 NSValue 中选项 1 和 2 需要额外的内存管理来确定何时释放数据,以便避免那些。选项 3 很好,碰巧 MapKit 提供了类
CLLocation
。这是 2 个坐标的示例。CLLocationCoordinate2D is not an object so they can not just be added to an array and returned. Since they are structs there are a few options,
malloc
an array the old fashion way and copy the struct data into the arraymalloc
a new struct pointer, copy the data, then store the pointer in anNSValue
Option 1 and 2 require additional memory management as to when to free the data so avoid those. Option 3 is good and it just so happens MapKit offers the class
CLLocation
. Here is an example of 2 coordinates.将位置对象添加到数组中并返回该数组。例如
Add your location objects to an array and return the array instead. e.g.