如何在 Objective-C 中传递多个参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/722651/
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
How do I pass multiple parameters in Objective-C?
提问by Atma
I have read several of the post about Objective-C method syntax but I guess I don't understand multiple names for a method.
我已经阅读了几篇关于 Objective-C 方法语法的帖子,但我想我不明白一个方法的多个名称。
I'm trying to create a method called getBusStopswith NSStringand NSTimeIntervalparameters and a return type of NSMutableArray. This is how I have constructed the method but it obviously gets errors at runtime:
我正在尝试创建一个getBusStops使用NSString和NSTimeInterval参数调用的方法,返回类型为NSMutableArray. 这就是我构造方法的方式,但它显然在运行时出错:
- (NSMutableArray *)getBusStops:(NSString *)busStop
(NSTimeInterval *)timeInterval;
I saw another example with a method:
我看到了另一个带有方法的示例:
-(NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
I don't understand why this method has a method name for each parameter. Should I do the same with something like:
我不明白为什么这个方法的每个参数都有一个方法名称。我应该对类似的事情做同样的事情:
- (NSMutableArray *)getBusStops:(NSString *)busStop
forTime:(NSTimeInterval *)timeInterval
采纳答案by Terry Wilcox
Objective-C doesn't have named parameters, so everything on the left side of a colon is part of the method name. For example,
Objective-C 没有命名参数,因此冒号左侧的所有内容都是方法名称的一部分。例如,
getBusStops: forTime:
is the name of the method. The name is broken up so it can be more descriptive. You could simply name your method
是方法的名称。名称已拆分,因此可以更具描述性。你可以简单地命名你的方法
getBusStops: :
but that doesn't tell you much about the second parameter.
但这并没有告诉你太多关于第二个参数的信息。
回答by E.M.
You need to delimit each parameter name with a ":" at the very least. Technically the name is optional, but it is recommended for readability. So you could write:
您至少需要用“:”分隔每个参数名称。从技术上讲,该名称是可选的,但建议使用以提高可读性。所以你可以写:
- (NSMutableArray*)getBusStops:(NSString*)busStop :(NSSTimeInterval*)timeInterval;
or what you suggested:
或者你的建议:
- (NSMutableArray*)getBusStops:(NSString*)busStop forTime:(NSSTimeInterval*)timeInterval;
回答by Alex Rozanski
Yes; the Objective-C method syntax is like this for a couple of reasons; one of these is so that it is clear what the parameters you are specifying are. For example, if you are adding an object to an NSMutableArrayat a certain index, you would do it using the method:
是的; 出于几个原因,Objective-C 方法语法是这样的;其中之一是要清楚您指定的参数是什么。例如,如果您NSMutableArray在某个索引处添加一个对象,您可以使用以下方法:
- (void)insertObject:(id)anObject atIndex:(NSUInteger)index;
This method is called insertObject:atIndex:and it is clear that an object is being inserted at a specified index.
这个方法被调用insertObject:atIndex:,很明显一个对象被插入到指定的索引处。
In practice, adding a string "Hello, World!" at index 5 of an NSMutableArraycalled arraywould be called as follows:
在实践中,添加一个字符串“Hello, World!” 在索引中的5NSMutableArray称为array将被称为如下:
NSString *obj = @"Hello, World!";
int index = 5;
[array insertObject:obj atIndex:index];
This also reduces ambiguity between the order of the method parameters, ensuring that you pass the object parameter first, thenthe index parameter. This becomes more useful when using functions that take a large number of arguments, and reduces error in passing the arguments.
这也减少了方法参数顺序之间的歧义,确保首先传递对象参数,然后是索引参数。这在使用带有大量参数的函数时变得更加有用,并减少了传递参数时的错误。
Furthermore, the method naming convention is such because Objective-C doesn't support overloading; however, if you want to write a method that does the same job, but takes different data-types, this can be accomplished; take, for instance, the NSNumberclass; this has several object creation methods, including:
此外,方法命名约定是这样的,因为Objective-C 不支持重载;但是,如果您想编写一个执行相同工作但采用不同数据类型的方法,则可以实现;以NSNumber班级为例;这有几种对象创建方法,包括:
+ (id)numberWithBool:(BOOL)value;+ (id)numberWithFloat:(float)value;+ (id)numberWithDouble:(double)value;
+ (id)numberWithBool:(BOOL)value;+ (id)numberWithFloat:(float)value;+ (id)numberWithDouble:(double)value;
In a language such as C++, you would simply overload the number method to allow different data types to be passed as the argument; however, in Objective-C, this syntax allows several different variants of the same function to be implemented, by changing the name of the method for each variant of the function.
在诸如 C++ 之类的语言中,您只需重载 number 方法以允许将不同的数据类型作为参数传递;然而,在 Objective-C 中,通过为函数的每个变体更改方法的名称,此语法允许实现同一函数的多个不同变体。
回答by sigjuice
The text before each parameter is partof the method name. From your example, the name of the method is actually
每个参数之前的文本是方法名称的一部分。从您的示例中,该方法的名称实际上是
-getBusStops:forTime:
Each : represents an argument. In a method call, the method name is split at the :s and arguments appear after the :s. e.g.
每个 : 代表一个参数。在方法调用中,方法名称在 :s 处拆分,参数出现在 :seg 之后
[getBusStops: arg1 forTime: arg2]
回答by VITROXMAN
for create method:
对于创建方法:
-(void)mymethods:(NSString *)aCont withsecond:(NSString *)a-second {
//method definition...
}
for call the method:
调用方法:
[mymethods:self.contoCorrente withsecond:self.asecond];
回答by Chaudhry Umair
(int) add: (int) numberOne plus: (int) numberTwo ;
(returnType) functionPrimaryName : (returnTypeOfArgumentOne) argumentName functionSecondaryNa
me:
我:
(returnTypeOfSecontArgument) secondArgumentName ;
as in other languages we use following syntax
void add(int one, int second)but way of assigning arguments in OBJ_cis different as described above
与其他语言一样,我们使用以下语法 voidadd(int one, int second)但分配参数的方式与OBJ_c上述不同

