现在的位置: 首页 > 综合 > 正文

ios学习—函数的书写

2013年10月10日 ⁄ 综合 ⁄ 共 2684字 ⁄ 字号 评论关闭

最近某个大厂要做内置,时间确实有点紧,不过要学的东西还是要学,要写的还是要写,最近再看ios,这个是本人收集的一个obj-c语法的一些东西,周末写一个关于android源码编译的博文,预告下!

object c 特殊的函数定义,很奇怪

相见下边

Q: 

I have read several of the post about Objective-C method syntax but I guess I don't understand multiple names for a method.

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:

- (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

大意: 读了很多object c 的代码,我不明白函数名字的写法,为什么每个参数都有一个名字

A:

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:

- (void)insertObject:(id)anObject atIndex:(NSUInteger)index;
Object - C 的函数特殊写法基于很多的原因,第一,这样写你可以清楚的知道每个参数表示什么,比如上边的函数, 表示在特定的位置添加对象。 

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:

NSString *obj = @"Hello, World!";
int index = 5;

[array insertObject:obj atIndex:index];

上面一个例子,很明显时在第5个索引位置添加了字符串对象。

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:

  • + (id)numberWithBool:(BOOL)value;
  • + (id)numberWithFloat:(float)value;
  • + (id)numberWithDouble:(double)value;

另外,这样写的原因还因为Object -C 不支持方法重载。 所以想要用同样的函数名称,可以这样实现。上边就是个很好的例子,使用相同的函数名称,具体参数引号前边的描述可以不同。  

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++ 我们可以很简单的用重载来实现,但是Object - C 的写法更灵活,只要修改每个参数的名称(引号前边的)就是不同的函数。

抱歉!评论已关闭.