本文介绍了具有2个返回值的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想调用一个返回两个值的方法

I want to call a method which returns two values

基本上可以说我的方法如下所示(想返回2个值)

basically lets say my method is like the below (want to return 2 values)

NSString* myfunc
{
   NSString *myString = @"MYDATA";
   NSString *myString2 = @"MYDATA2";
   return myString;
   return myString2;
}

所以当我叫它时,我会用吗?

So when i call it, i would use??

NSString* Value1 = [self myfunc:mystring];
NSString* Value2 = [self myfunc:mystring2];

我想我做错了什么,有人可以帮我吗?谢谢

I guess im doing something wrong with it, can anyone help me out?Thanks

推荐答案

您有一些选择:

  • NSArray :仅返回一个数组.很简单.
  • 指针:传入两个指针,然后写入它们,而不返回任何内容.确保检查NULL!
  • 结构:创建一个具有两个字段的结构,每个要返回的内容都一个,然后返回该结构中的一个.
  • 对象:相同的结构,但创建完整的NSObject子类.
  • NSDictionary :类似于NSArray,但不再需要使用值的魔术排序.
  • NSArray: Just return an array. Pretty simple.
  • Pointers: Pass in two pointers, and write to them instead of returning anything. Make sure to check for NULL!
  • Structure: Create a struct that has two fields, one for each thing you want to return, and return one of that struct.
  • Object: Same a structure, but create a full NSObject subclass.
  • NSDictionary: Similar to NSArray, but removes the need to use magic ordering of the values.

这篇关于具有2个返回值的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 13:31
查看更多