ios学习---函数的书写
来源:互联网 发布:网络图书馆 编辑:程序博客网 时间:2024/05/17 15:20
最近某个大厂要做内置,时间确实有点紧,不过要学的东西还是要学,要写的还是要写,最近再看ios,这个是本人收集的一个obj-c语法的一些东西,周末写一个关于android源码编译的博文,预告下!
相见下边
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 的写法更灵活,只要修改每个参数的名称(引号前边的)就是不同的函数。
- ios学习---函数的书写
- 宏函数的书写
- Strcopy函数的书写
- 函数书写的规则
- 函数书写的一点总结
- matlab函数注释的书写
- Linux设备驱动开发学习笔录-(init函数和exit函数的书写)
- iOS 开发 OC编程 方法的书写
- iOS--单例的正确书写
- iOS 代码书写的整洁与美化
- 求助 ASP函数的调用以及书写?
- JS 内部调用函数 的书写方式
- 书写Makefile之Makefile的内置函数
- 用总结书写学习的生命
- opencv学习--项目代码的书写流程
- IOS学习---C语言程序书写格式小建议
- iOS书写代码规范
- IOS模型书写格式
- 制作kernel内核时make menuconfig出错关于 ncurses
- 二叉排序树的应用(基于二叉排序树的个人通信录)
- 16个HTML5 框架、模板以及生成工具
- /$ORACLE_BASE/下没有admin路径
- Javascript undefined vs null
- ios学习---函数的书写
- uva 301 - Transportation
- 20个代码生成软件
- oracle10g创建数据库的方法
- Javascript中出现内存泄漏的主要原因是什么
- PHP 安装包 VC9 VC6 区别是什么
- ExpandableListView 使用方式
- [java]中代码注释问题
- oracle登录方式