iOS开发学习心得(1)----关于Objectvie-C

来源:互联网 发布:大数据整合 编辑:程序博客网 时间:2024/05/19 10:08

最近由于项目需要,开始接触iOS的开发,在此记录一下,方便以后查看。

说到iOS,不得不提的就是Objectvie-C,顾名思义,它是在C的基础上扩展的一门面向对象的语言。

在Objective-C中,类被生命为 Interface 类的生命被包含在@Interface...@end之中,而不是C++中的 class xxx{...}; 。类的实现包含在 @Inplemnets...@end之中。

一下是一段 Objective-C 与 C++ 的语法对照:

Objective-C:

@interface MyObject : NSObject {    int memberVar1; // 實體變數    id  memberVar2;}+(return_type) class_method;            // 類方法 -(return_type) instance_method1;        // 實體方法-(return_type) instance_method2: (int) p1;-(return_type) instance_method3: (int) p1 andPar: (int) p2;@end
Objective-C:的实现:

@implementation MyObject +(return_type) class_method {    .... //method implementation}-(return_type) instance_method1 {     ....}-(return_type) instance_method2: (int) p1 {    ....}-(return_type) instance_method3: (int) p1 andPar: (int) p2 {    ....}@end


C++:

class MyObject : public NSObject {    int memberVar1;  // 實體變數    void * memberVar2;   public:    static return_type class_method(); // 類方法     return_type instance_method1();    // 實體方法    return_type instance_method2( int p1 );    return_type instance_method3( int p1, int p2 );}


在Objective-C 类中方法前的 +(加号) 表示类方法,类似于C++中的 static 方法。-(减号) 表示实体方法。

方法的参数也不像其他语言中的声明在括号内, 而是以 :(冒号)代表参数传递。方法的调用方式也跟C++中有很大的区别。

Objective-C中,调用一个方法 语法如下:

[obj method: argument];

C++:

obj->method(argument);

笔者功力有限,这里只是及其简单的介绍了一下Objective-C,由于笔者对C++比较熟悉,在学习Objective-C中,经常拿它跟C++进行比较,这样虽然可以较快的上手,但是Objective-C中还是有很多与C++中不同的特性,所以想要深入了解Objective-C,还是需要较系统的学习Objectvie-C。


原创粉丝点击