您可以在Objective-C中返回两个参数吗? [英] Can you return two arguments in objective-c?
问题描述
只是想知道您是否可以在目标c方法中返回两个参数,例如,我想返回一个笛卡尔坐标(x,y),基本上是两个整数.创建类并返回实例的唯一方法是什么?还有别的我没想到的方法吗,这里是初学者.
Just wondering if you can return two arguments in an objective c method, for example I'd like to return a cartesian coordinate (x,y), basically two ints. is the only way to do this by creating a class and returning an instance? Is there some other way I'm not thinking of, kind of a beginner here.
(在语法上也将有所帮助.)
(Syntactical help would also be appreciated.)
谢谢
尼克
推荐答案
您只能从函数中返回一个值(就像C一样),但是该值可以是任何值,包括结构.而且由于可以按值返回结构,因此可以定义一个这样的函数(借用您的示例):
You can only return one value from a function (just like C), but the value can be anything, including a struct. And since you can return a struct by value, you can define a function like this (to borrow your example):
-(NSPoint)scalePoint:(NSPoint)pt by:(float)scale
{
return NSMakePoint(pt.x*scale, pt.y*scale);
}
这仅真正适用于小型/简单结构.
This is only really appropriate for small/simple structs.
如果要返回多个新的 object ,则函数应采用指向对象指针的指针,因此:
If you want to return more than one new object, your function should take pointers to the object pointer, thus:
-(void)mungeFirst:(NSString**)stringOne andSecond:(NSString**)stringTwo
{
*stringOne = [NSString stringWithString:@"foo"];
*stringTwo = [NSString stringWithString:@"baz"];
}
这篇关于您可以在Objective-C中返回两个参数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!