c和类c用惯了,objective c 确一点也不像c, 非常简单,只是习惯需要适应一下
1.基础
#import相当于#include, 但是#import保证只编译一次,其实相当如#ifndef ..#include
NSLog,打印一行文字,oc中字符必须以@开始
BOOL 的值是 YES和NO
2.面向对象
在这做个记号
枚举定义
typedef enum{
kPoint,
kPolyline,
kPolygon
}ShapeType;
结构体定义
typedef struct{
Point center;
int radius;
}ShapeStruct
id 相当于 void*,泛指针
self 相当于 this
IBOutlet和IBAction 相当于void
+ 相当于 public static
- 相当于 public
oc到现在为止可以认为所有函数都是public的,没有private,protect方法,一定要实现也是可以实现的
@interface Controller : NSObject
{
NSString *something;
}
+ (void)thisIsAStaticMethod;
- (void)thisIsAnInstanceMethod;
@end
@interface Controller (Private)
- (void)thisIsAPrivateMethod;
@end
只要在头文件或者实现类里面添加一个interface,这个Interface的名字跟头文件里面的一样,只是后面加上了(Private)。现在在这个interface中声明的方法就是私有方法了。
@interface ... @end 相当于 class A: classB{}
@implementation... @end 相当于c++的函数体
可以在@implementation中定义@interface中没有的函数,这样也可以实现类似c++中私有方法
函数体
- (IBAction)generate:(id)sender;
相当于
public:
void generate(void* sender);
调用函数体
[objectname functionname:value]; 相当于 objectname.functioname(value);
[classname param] ; 相当于 new classname(param);
在 Objective-C 中的类实现中经常看到这两个关键字 ”self” 和 ”super”,以以前 oop 语言的经验,拿 c++ 为例,self 相当于 this,super 相当于调用父类的方法