1,源代码文件扩展名
在c语言中,头文件用 .h 来表示,而其实现文件时用 .c 来表示。
在c++中,头文件时用 .h 来表示,而其实现文件时用 .cpp 来表示。
而在Objective-c简称oc,oc中头文件是用 .h 来表示,而其实现文件时用 .m 来表示。
另外在oc与c++混合编程中,头文件时用 .h 来表示,而其实现文件时用 .mm 来表示。
2, NSLog就类似于C语言中的printf,在终端输出信息。
NSLog(@"Hello Objective-C");
如果不加上‘@’符号,这个字符串就是C语言的字符串类型,加上'@'是为了告诉编译器这是OC类型的字符串。
NSLog(@"The number is %d",1000);
NSLog(@"Hello,%s","China");
这上面基本就和C语言是差不多的,%s代表字符串,%d代表整形
NSString *name = @"nihao"
NSLog(@"Hello %@",name);
输出一个字符串类型的对象,NSString是继承于NSObject,所以只要是继承于NSObject类型的,都可以使用'@'作为占位符来输出该对象。
NSLog(@"NSObject is %@",[[NSObject alloc] init]); [[NSObject alloc] init]表示申请一块内存空间,并初始化该内存空间。
这边会输出 "NSObject is 0x123456",输出的这个对象的一个地址。
当你用"%@"来作为输出时的格式控制符,它都会去执行NSObject的一个description方法。
+(NSString *)description
NSLog(@"NSObject is %@",[[[NSObject alloc] init] description] );
这边返回的是这个对象的地址。
3,函数调用
OC是完全兼容C的函数调用方法,这里我们只看OC的函数调用
NSString *str = [NSString stringWithFormat:@"Hello %d",100];
NSLog(@"str was %@",str);
4,引入头文件
OC引入头文件是用import,类似于C语言中include。
5,创建OC的一个类
创建一个类
@interface 类名:继承于哪个类
//定义的一些方法
@end
实现这个类的方法
@implementation 实现的类名
//实现的方法
@end
'-'表示创建的方法是一个公开的实例方法。
@interface Hello : NSObject{
int num;
}
-(id)init;
-(void)sayHello;
@end
@implementation Hello
-(instancetype)init{
self = [super init];//继承父类的初始化方法
if (self){
num = 200;
}
return self;
}
-(void)sayHello{
NSLog(@"Test the OC class");
NSLog(@"num is %d",num);
}
@end
下面我在一个function里面想调用这个类的时候,假设在main函数中需要调用
int main(int argc,char * argv[])
{
Hello *h = [[Hello alloc] init];
[h sayHello];
}
6, OC的Set和Get方法
新建一个OC类型的People类,继承于NSObject类,创建成功后,有People.h和People.m文件。
@interface People : NSObject{
int _age;
}
@property int age;//只要我们写了这样一个属性age,系统会自动帮我们生成一个set和get age的方法,这是OC的一个特色
@end
int main(int argc,char * argv[])
{
People *p = [[People alloc] init];
p.age = 10;
NSLog(@"p.age=%d",p.age);
[p setAge:20]
NSLog(@"p.age=%d",[p age]);
}
上面那种是系统自动生成的setAge和age方法,分别表示设定以及得到age值。
下面可以通过在People.m文件中手动修改setAge和age方法
@implement People
-(void)setAge:(int) age{
_age = age;
}
-(int)age{
return _age;
}
@end
7,工厂方法(就是一系列静态的方法)
在People.h中写下类的定义
@interface People : NSObject{
int _age;
NSString *_name;
}
+(People *)PeopleWithAge:(int) age andName:(NSString*) name;
-(id)initWithAge:(int) age andName:(NSString *)name;
-(int)getAge;
-(NSString*)getName;
@end
在People.m文件中写下该类的实现
@implement People
+(People *)PeopleWithAge:(int) age andName:(NSString*)name{
return [[People alloc] initWithAge:age andName:name];
}
-(instancetype)initWithAge:(int) age andName:(NSString*) name{
self = [super init];//继承父类的初始化方法
if (self){
_age =age;
_name = name;
}
return self;
}
-(int)getAge{
return _age;
}
-(NSString *)getName{
return _name;
}
@end
在main函数中使用的时候
int main(int argc,char *argv[ ])
{
People *p = [People peopleWithAge:20 andName:@"xiaoli"];
NSLog(@"age is %d,name is %s",[p getAge],[p getName]);
}
8,
OC数组
NSArray *arr = @[@"Hello",@"china",@"time"];
for(int i=0;i<[arr count];i++)
{
NSLog(@"%@",[arr objectAtIndex:i])
}
//另一种使用工厂方法来进行创建一个数组
NSArray *arr = [NSArray arrayWithObjects:@"Hello",@"china",@"time",nil];
//OC可变数组
NSMutableArray *arr = [[NSMutableArray alloc] init];
for(int i=0;i<100;i++)
{
[arr addObject:[NSString stringWithFormat:@"Item %d",i]];//实现动态向arr数组里添加数组元素。
}
NSLog(@"%@",arr);
9,
OC字典
//先创建一对键值对
NSDictionary *dict = @{@"name":@"zhangshan",@"sex":@"male"};
NSLog(@"%@",dict);
NSLog(@"%@",[dict objectForKey:@"name"]);
//读取plist文件,plist里面存放的数据都是键值对形式。
NSDictionary *dict = [ NSDictionary dictionaryWithContentOfFile[NSBundle mainBundle] pathForResource:@"data",ofType:@"plist"];
NSLog(@"%@",[dict objectForKey:@"name"]);
//OC可变字典
NSMutableDictionary *dict =[[NSMutableDictionary alloc] init];
[dict setObject:@"zhangsan" forKey:"name"];--->通过这种方式可以修改plist里面的数据。
NSLog(@"%@",[dict objectForKey:@"name"]);
10,代码块(类似于C语言中的函数指针)
定义一个代码块如下
void (^max)(int,int);
typedef void (^ SayHello)();
int main(int argc,char * argv[])
{
max = ^(int a,int b){
return a>b ? a:b;
}
printf("max is %d\n",max(2,3));
}
SayHello sh = ^(){
printf("Hello Obj Blocked\n");
}
sh();
11, OC中的协议(protocol)
新建一个protocol,IProtocol.h
@protocol IPeople <NSObject>
-(int)getAge;
-(NSString *)getName;
@end
新建一个Man类,打开Man.h
@interface Man : NSObject<IPeople>--->IPeople这个接口
//定义两个方法。
-(int)getAge;
-(NSString *)getName;
@end
打开Man.m
@implement Man
-(int)getAge{
return 20;
}
-(NSString *)getName{
return @"zhangsan";
}
@end
12,OC中的Delegate
IProtocol.h
@protocol IPeople <NSObject>
-(void)setAge:(int)age;
-(int)getAge;
-(NSString *)getName;
@end
Man.h --->IPeople这个接口
#import "ManDelegate.h"
@interface Man : NSObject<IPeople>{
int _age;
}
//定义两个方法。
-(void)setAge:(int)age;
-(int)getAge;
-(NSString *)getName;
@property id<ManDelegate> deledate;
@end
Man.m
@implement Man
-(void)setAge:(int)age{
if(age!=_age){
}
_age = age;
}
-(int)getAge{
return age;
}
-(NSString *)getName{
return @"zhangsan";
}
@end
新建一个ManDelegate.h
@protocol ManDelegate <NSObject>
-(void) onAgeChanged:(int)age;
@end
13,OC中类型判断
@interface A : NSObject{
@end
@implement A
@end
@interface AA : NSObject{
@end
@implement AA
@end
A *a = [[A alloc] init];
NSLog(@"%d",[a isKindOfClass[A]]);---->是否属于A的类型,是返回1,不是返回0
14,OC中异常处理
@try{
}
@catch(NSException *exception){
}
@finally{
}