如何从方法返回多个值

时间:2020-12-29 02:05:04

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)方法,我想从该方法返回多个位置(这些位置在另一个方法中使用)我的方法看起来像这样,

-(CLLocationCoordinate2D) addressLocation{
 - --------
 ----------
 return location;
 }

is it possible to return multiple locations (I mean return location1 , return location2 . . ..) ?? please help me thanks

是否有可能返回多个位置(我的意思是返回location1,返回location2 .. ..)??请帮我谢谢

2 个解决方案

#1


4  

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.
}

#2


7  

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,

CLLocationCoordinate2D不是一个对象,因此它们不能只是添加到数组并返回。由于它们是结构,因此有几种选择,

  1. malloc an array the old fashion way and copy the struct data into the array
  2. malloc数组旧方式并将struct数据复制到数组中

  3. malloc a new struct pointer, copy the data, then store the pointer in an NSValue
  4. malloc一个新的struct指针,复制数据,然后将指针存储在NSValue中

  5. Create a class that has the same properties as the fields of the struct, and add that to an array
  6. 创建一个与结构字段具有相同属性的类,并将其添加到数组中

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.

选项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;
}

#1


4  

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.
}

#2


7  

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,

CLLocationCoordinate2D不是一个对象,因此它们不能只是添加到数组并返回。由于它们是结构,因此有几种选择,

  1. malloc an array the old fashion way and copy the struct data into the array
  2. malloc数组旧方式并将struct数据复制到数组中

  3. malloc a new struct pointer, copy the data, then store the pointer in an NSValue
  4. malloc一个新的struct指针,复制数据,然后将指针存储在NSValue中

  5. Create a class that has the same properties as the fields of the struct, and add that to an array
  6. 创建一个与结构字段具有相同属性的类,并将其添加到数组中

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.

选项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;
}