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 getBusStops
with NSString
and NSTimeInterval
parameters and a return type of NSMutableArray
. This is how I have constructed the method but it obviously gets errors at runtime:
我正在尝试创建一个名为getbusstop的方法,它使用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
6 个解决方案
#1
102
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.
但这并没有告诉你第二个参数。
#2
137
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;
#3
62
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 NSMutableArray
at 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 NSMutableArray
called array
would be called as follows:
在实践中,在NSMutableArray调用数组的索引5中添加一个字符串“Hello, World!”
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, then the 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 NSNumber
class; this has several object creation methods, including:
此外,方法命名约定是这样的,因为Objective-C不支持重载;但是,如果您想要编写一个方法来完成相同的工作,但是需要使用不同的数据类型,那么就可以完成;例如,NSNumber类;这有几个对象创建方法,包括:
+ (id)numberWithBool:(BOOL)value;
- +(id)numberWithBool:(BOOL)值;
+ (id)numberWithFloat:(float)value;
- +(id)numberWithFloat:(浮动)值;
+ (id)numberWithDouble:(double)value;
- +(id)numberWithDouble:(双)值;
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++这样的语言中,您只需要重载数字方法,以允许将不同的数据类型作为参数传递;但是,在Objective-C中,这个语法允许通过更改函数的每个变体的方法的名称来实现相同函数的几个不同变体。
#4
23
The text before each parameter is part of 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中被拆分,并且在:s之后出现参数。如。
[getBusStops: arg1 forTime: arg2]
#5
4
for create method:
创建方法:
-(void)mymethods:(NSString *)aCont withsecond:(NSString *)a-second { }
for call the method:
调用方法:
[mymethod:self.contoCorrente withsecond:self.asecond];
#6
-2
(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_c
is different as described above
和其他语言一样,我们使用下面的语法void add(int one, int second),但是在OBJ_c中分配参数的方式与上面描述的不同。
#1
102
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.
但这并没有告诉你第二个参数。
#2
137
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;
#3
62
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 NSMutableArray
at 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 NSMutableArray
called array
would be called as follows:
在实践中,在NSMutableArray调用数组的索引5中添加一个字符串“Hello, World!”
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, then the 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 NSNumber
class; this has several object creation methods, including:
此外,方法命名约定是这样的,因为Objective-C不支持重载;但是,如果您想要编写一个方法来完成相同的工作,但是需要使用不同的数据类型,那么就可以完成;例如,NSNumber类;这有几个对象创建方法,包括:
+ (id)numberWithBool:(BOOL)value;
- +(id)numberWithBool:(BOOL)值;
+ (id)numberWithFloat:(float)value;
- +(id)numberWithFloat:(浮动)值;
+ (id)numberWithDouble:(double)value;
- +(id)numberWithDouble:(双)值;
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++这样的语言中,您只需要重载数字方法,以允许将不同的数据类型作为参数传递;但是,在Objective-C中,这个语法允许通过更改函数的每个变体的方法的名称来实现相同函数的几个不同变体。
#4
23
The text before each parameter is part of 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中被拆分,并且在:s之后出现参数。如。
[getBusStops: arg1 forTime: arg2]
#5
4
for create method:
创建方法:
-(void)mymethods:(NSString *)aCont withsecond:(NSString *)a-second { }
for call the method:
调用方法:
[mymethod:self.contoCorrente withsecond:self.asecond];
#6
-2
(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_c
is different as described above
和其他语言一样,我们使用下面的语法void add(int one, int second),但是在OBJ_c中分配参数的方式与上面描述的不同。