从 Objective-C 中的方法返回多个对象

发布于 2024-09-19 01:28:12 字数 143 浏览 3 评论 0原文

我有一个方法,它接受 NSMutableArray 作为参数,我希望它返回该数组、该方法中创建的另一个数组以及该方法创建的 int 。我意识到这可以通过创建一个包含所有这些对象的数组并返回该数组,然后将它们从方法外部的数组中删除来完成,但是还有其他方法可以返回多个对象吗?

I have a method that takes an NSMutableArray as a parameter, and I would like it to return that array, another array that is created in the method, and an int created by the method. I realize this could be done by making an array that had all these objects in it, and returning that, then removing them from the array outside the method, but is there another way to return multiple objects?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

街角迷惘 2024-09-26 01:28:12

传回多个值的典型方法是:

  • 通过引用添加额外的参数(例如,通常传递 NSErrors
    通过这种方式)
  • 将它们传递回其他一些结构或对象,通常是
    NSDictionary 或自定义类

以上对于许多情况来说都是很好的解决方案,但这里是另一个在其他情况下可能效果最好的解决方案: 在

您的方法中添加一个块:

- (void)myMethodWithMultipleReturnObjectsForObject:(id)object returnBlock:(void (^)(id returnObject1, id returnObject2))returnBlock
{
    // do stuff

    returnBlock(returnObject1, returnObject2);
}

然后使用这样的方法:

[myMethodWithMultipleReturnObjectsForObject:object returnBlock:^(id returnObject1, id returnObject2) {
    // Use the return objects inside the block
}];

上例中的返回对象仅是可在块内使用,因此如果您想将它们保留在块外使用,只需设置一些 __block 变量即可。

// Keep the objects around for use outside of the block
__block id object1;
__block id object2;
[myMethodWithMultipleReturnObjectsForObject:object returnBlock:^(id returnObject1, id returnObject2) {
    object1 = returnObject1;
    object2 = returnObject2;
}];

The typical ways to pass back multiple values are to:

  • Add extra parameters by reference (ex. typically NSErrors are passed
    this way)
  • Pass them back in some other struct or object, typically an
    NSDictionary or a custom class

The above are good solutions for many situations, but here is another solution that may work best in other situations:

Add a block to your method:

- (void)myMethodWithMultipleReturnObjectsForObject:(id)object returnBlock:(void (^)(id returnObject1, id returnObject2))returnBlock
{
    // do stuff

    returnBlock(returnObject1, returnObject2);
}

Then use the method like this:

[myMethodWithMultipleReturnObjectsForObject:object returnBlock:^(id returnObject1, id returnObject2) {
    // Use the return objects inside the block
}];

The return objects in the above example are only usable within the block, so if you want to keep them around for use outside the block, just set some __block vars.

// Keep the objects around for use outside of the block
__block id object1;
__block id object2;
[myMethodWithMultipleReturnObjectsForObject:object returnBlock:^(id returnObject1, id returnObject2) {
    object1 = returnObject1;
    object2 = returnObject2;
}];
演多会厌 2024-09-26 01:28:12

使用 NSDictionary 返回多个值是 Obj-C 中执行此操作的惯用方法。

方法签名看起来像这样:

-(NSDictionary *)doSomeStuffThatReturnsMultipleObjects;

并且您将需要在适当的文件中定义字典键。

// Header File
extern NSString *const JKSourceArray;
extern NSString *const JKResultsArray;
extern NSString *const JKSomeNumber;

// Implementation File
NSString *const JKSourceArray = @"JKSourceArray";
NSString *const JKResultsArray = @"JKResultsArray";
NSString *const JKSomeNumber = @"JKSomeNumber";

与使用数组相比的优点是元素的顺序和元素的存在/不存在并不重要,如果您想返回其他对象,则将来更容易扩展。它也比通过引用传递更加灵活和可扩展。

Using an NSDictionary to return multiple values is the customary way to do this in Obj-C.

The method signature would look something like this:

-(NSDictionary *)doSomeStuffThatReturnsMultipleObjects;

and you are going to want to define your dictionary keys in the appropriate files.

// Header File
extern NSString *const JKSourceArray;
extern NSString *const JKResultsArray;
extern NSString *const JKSomeNumber;

// Implementation File
NSString *const JKSourceArray = @"JKSourceArray";
NSString *const JKResultsArray = @"JKResultsArray";
NSString *const JKSomeNumber = @"JKSomeNumber";

The advantage over using an array is that the the order of elements and the presence/absence of elements doesn't matter, making it far easier to extend in the future, if you want to return additional objects. It's also far more flexible and extensible than passing by reference.

兲鉂ぱ嘚淚 2024-09-26 01:28:12

另一种方法是让用户传递一个他们想要用来保存数据的指针。下面是一个示例,它返回一个数组并使用指针为您提供一个 int 值和另一个数组。编辑好,这是我自己刚刚测试过的工作版本:

- (NSMutableArray*)doStuff:(int*)container returnedArray:(NSMutableArray*)arrayContainer{
   int a = 4;
   *container = a;
   [arrayContainer removeAllObjects];
   [arrayContainer addObject:@"object"];
   return [NSMutableArray arrayWithObjects:@"object",nil];
}

这样你就可以这样说:

int value = 0;
NSMutableArray* new = [NSMutableArray array];
[self doStuff:&value returnedArray:new];

它基本上就像退货一样!

Another way you can do it is have the user pass a pointer which they want to use to hold the data. Here is a sample that returns an array and uses pointers to give you an int value and another array. EDIT ok here is the working version I just tested it myself:

- (NSMutableArray*)doStuff:(int*)container returnedArray:(NSMutableArray*)arrayContainer{
   int a = 4;
   *container = a;
   [arrayContainer removeAllObjects];
   [arrayContainer addObject:@"object"];
   return [NSMutableArray arrayWithObjects:@"object",nil];
}

That way you could say like:

int value = 0;
NSMutableArray* new = [NSMutableArray array];
[self doStuff:&value returnedArray:new];

And it basicly works like a return!

浅忆流年 2024-09-26 01:28:12

您可能需要考虑返回一个结构。

typedef struct {
    NSMutableArray *array1;
    NSArray *array2;
    NSInteger integer;
} MyAwesomeReturnValue;

您的方法现在看起来像这样:

- (MyAwesomeReturnValue)myAwesomeMethod:(NSMutableArray *)parameter
{
    MyAwesomeReturnValue retval;
    retval.array1 = parameter;
    retval.array2 = [NSArray array];
    retval.integer = 5;

    return retval;
}

您可以像这样使用它:

- (void)anotherAwesomeMethod
{
    NSMutableArray *array = [NSMutableArray array];
    MyAwesomeReturnValue returnedValue = [self myAwesomeMethod:array];

    NSLog(@"%@", returnedValue.array1);
    NSLog(@"%@", returnedValue.array2);
    NSLog(@"%d", returnedValue.integer);
}

希望有所帮助。 ;)

You might want to consider returning a struct.

typedef struct {
    NSMutableArray *array1;
    NSArray *array2;
    NSInteger integer;
} MyAwesomeReturnValue;

Your method could now look like this:

- (MyAwesomeReturnValue)myAwesomeMethod:(NSMutableArray *)parameter
{
    MyAwesomeReturnValue retval;
    retval.array1 = parameter;
    retval.array2 = [NSArray array];
    retval.integer = 5;

    return retval;
}

And you'd use it like:

- (void)anotherAwesomeMethod
{
    NSMutableArray *array = [NSMutableArray array];
    MyAwesomeReturnValue returnedValue = [self myAwesomeMethod:array];

    NSLog(@"%@", returnedValue.array1);
    NSLog(@"%@", returnedValue.array2);
    NSLog(@"%d", returnedValue.integer);
}

Hope that helped. ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文