如何从一个方法返回多个值

发布于 2024-11-18 02:00:13 字数 300 浏览 3 评论 0原文

我有 -(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

影子的影子 2024-11-25 02:00:13

CLLocationCooperative2D 不是一个对象,因此不能将它们添加到数组中并返回。因为它们是结构体,所以有几个选项,

  1. malloc以旧方式分配一个数组,并将结构数据复制到数组
  2. malloc一个新的结构体指针,复制数据,然后将指针存储在 NSValue 中
  3. 创建一个与结构体字段具有相同属性的类,并将其添加到数组

选项 1 和 2 需要额外的内存管理来确定何时释放数据,以便避免那些。选项 3 很好,碰巧 MapKit 提供了类 CLLocation。这是 2 个坐标的示例。

-(NSArray*) addressLocations
{
    CLLocationCoordinate2D coord1 = CLLocationCoordinate2DMake(1.00, 1.00);
    CLLocationCoordinate2D coord2 = CLLocationCoordinate2DMake(2.00, 2.00);

    CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:coord1.latitude longitude:coord1.longitude];
    CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:coord2.latitude longitude:coord2.longitude];

    NSArray *array = [NSArray arrayWithObjects:loc1, loc2, nil];

    [loc1 release];
    [loc2 release];

    return array;
}

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,

  1. malloc an array the old fashion way and copy the struct data into the array
  2. malloc a new struct pointer, copy the data, then store the pointer in an NSValue
  3. Create a class that has the same properties as the fields of the struct, and add that to an array

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.

-(NSArray*) addressLocations
{
    CLLocationCoordinate2D coord1 = CLLocationCoordinate2DMake(1.00, 1.00);
    CLLocationCoordinate2D coord2 = CLLocationCoordinate2DMake(2.00, 2.00);

    CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:coord1.latitude longitude:coord1.longitude];
    CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:coord2.latitude longitude:coord2.longitude];

    NSArray *array = [NSArray arrayWithObjects:loc1, loc2, nil];

    [loc1 release];
    [loc2 release];

    return array;
}
[旋木] 2024-11-25 02:00:13

将位置对象添加到数组中并返回该数组。例如

-(NSArray*) addressLocation{
   ...  // Set your locations up
   NSArray *array = [NSArray arrayWithObjects:location1, location2, nil];
   ... // Do any additional processing and return array.
}

Add your location objects to an array and return the array instead. e.g.

-(NSArray*) addressLocation{
   ...  // Set your locations up
   NSArray *array = [NSArray arrayWithObjects:location1, location2, nil];
   ... // Do any additional processing and return array.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文