xcode 如何使用Objective-C将同一函数的两个返回值显示到两个标签中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7214715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:49:19  来源:igfitidea点击:

How to show two return values of same function into two labels with Objective-C?

iphonexcode

提问by Ashish

I am new to Objective-C. I have written a function that returns two values. Now I would like to print it into two separate labels, how I can do it?

我是 Objective-C 的新手。我编写了一个返回两个值的函数。现在我想把它打印成两个单独的标签,我该怎么做?

-(NSString *)abc:(NSInteger)weeks year:(NSInteger)year{

............

return ca , da ;

}

and when I call this function like

当我调用这个函数时

resultLabel1.text = [self abc year:year];  //output show the value of da

now I want to show the value of ca into resultLabel1.text and da into resultLabel2.text

现在我想将 ca 的值显示到 resultLabel1.text 中,将 da 显示到 resultLabel2.text 中

is it possible?

是否可以?

回答by Jules

You can only return a single valuefrom any method in C and C-derived languages. So you simply need to return a single value that represents both values. You can achieve this by making use of a NSDictionary.

您只能从 C 和 C 派生语言中的任何方法返回单个值。所以你只需要返回一个代表两个值的值。您可以通过使用NSDictionary来实现这一点。

So make it:

所以让它:

-(NSDictionary *)abc:(NSInteger)weeks year:(NSInteger)year{

     NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
           ca, @"object1", 
           da, @"object2", nil];

    return dict;

}

Another way is to return an NSArray:

另一种方法是返回一个NSArray

- (NSArray *)abc:(NSInteger)weeks year:(NSInteger)year {

     NSArray *myArray = [NSArray arrayWithObjects:da, ca, nil];
     return myArray;
}

And you can then use these values as:

然后您可以将这些值用作:

NSArray *myArray = [self abc:2 year:2004];
textLabel.text = (NSString *)[myArray objectAtIndex:0];
textLabel2.text = (NSString *)[myArray objectAtIndex:1];

回答by progrmr

As Jules points out a method can "return" only a single value. However, you have several options for returning multiple values:

正如 Jules 指出的那样,一个方法只能“返回”一个值。但是,您有多种返回多个值的选项:

  1. Return a pointer to an object, where the object contains multiple values. The object can be an NSArray, NSDictionary or you own class. Jules answergave some examples of this.

  2. Pass multiple pointers in your parameters, and the method can store results in the object or variable pointed to. See example here.

  3. Return a struct that has multiple fields. There's an example here.

  1. 返回一个指向对象的指针,该对象包含多个值。该对象可以是 NSArray、NSDictionary 或您自己的类。 Jules 的回答给出了一些例子。

  2. 在参数中传递多个指针,该方法可以将结果存储在指向的对象或变量中。请参阅此处的示例

  3. 返回具有多个字段的结构。这里有一个例子

回答by Niels Castle

I'd use a NSDictionary to return multiple values from a method. In the dictionary each value is named and referenced by a key. The keys in this example are "ca" and "da" and the values are both a short string of text.

我会使用 NSDictionary 从一个方法返回多个值。在字典中,每个值都由一个键命名和引用。本例中的键是“ca”和“da”,值都是一个短文本字符串。

-(NSDictionary *) abc: (NSInteger) weeks year:(NSInteger) year {

   NSString* ca = [NSString stringWithFormat:@"Week is %d", weeks];
   NSString* da = [NSString stringWithFormat:@"Year is %d", year];

   return [[NSDictionary alloc] initWithObjectsAndKeys:ca, @"ca", da, @"da", nil];
}

Call the method and pick out the returned values with code like this:

调用该方法并使用如下代码挑选返回值:

   NSInteger weekParam = @"52".integerValue;
   NSInteger yearParam = @"2011".integerValue;

   NSDictionary *result = [self abc:weekParam  year:yearParam];

   NSLog(@"ca has value: %@", [result objectForKey:@"ca"]);
   NSLog(@"da has value: %@", [result objectForKey:@"da"]);

You log should have the following lines added:

您的日志应该添加以下几行:

ca has value: Week is 52
da has value: Year is 2011

回答by pkamb

You can "return" multiple objects as parameters in a block:

您可以“返回”多个对象作为块中的参数:

- (void)method {
    [self returnMultipleObjectsWithCompletion:^(NSString *firstString, NSString *secondString) {
        NSLog(@"%@ %@", firstString, secondString);
    }];
}

- (void)returnMultipleObjectsWithCompletion:(void (^)(NSString *firstString, NSString *secondString))completion {
    if (completion) {
        completion(@"firstReturnString", @"secondReturnString");
    }
}

回答by Dancreek

You will have to return an NSArray or NSDictionary with the two values.

您将不得不返回带有两个值的 NSArray 或 NSDictionary。