block和协议和foundation

block

1)用来保存一段代码

2)block的标志:^

例子:

int sum(int a, int b){

return a+b;

}

int (*p)(int, int) = sum;

int (^sumblock)(int, int) = ^(int a, int b){

  return a+b;

};

int c = sumblock(10, 11);

3)定义block变量:

int (^sumBlock)(int, int);

void(^myBlock)();

 如何利用block封装代码:

^(int a, int b){

return a-b;

};

^(){

NSLog(@“————-”);

};

^{

NSLog(@“—————-”);

};

block访问外部变量:

block内部可以访问外部变量

默认情况下,block内部不能修改外面的局部变量

给局部变量加上__block关键字,这个局部变量就可以在block内部修改

利用typedef定义block类型:

typedef int (^MyBlock)(int, int); //以后就可以利用MyBlock这种类型来定义block变量

Myblock b3 = ^(int a, int b){

return a - b;  

};  //一条赋值语句结束后要加;

17)#import “MyProtocol” //只要一个类遵守了某一份协议,就能拥有这份协议中的所有方法申明

// 继承父类 :

//   遵守协议 <>

        @interface Person:NSObject <MyProtocol>

@required 要求实现,不实现就会发出警告

@optional 不要求实现

默认情况下是@required

基本用途:

》可以用来申明一大堆方法(不能申明成员变量)

》只要某个遵守了这个协议,就相当于拥有这个协议中的所有方法申明

》只要父类遵守了某个协议,就相当于子类也遵守类

要求obj3保存点对象必须遵守是MyProtocol这个协议

NSObject<MyProcol> *obj3 = [[NSObject a alloc] init];

协议的定义:

@protocol 协议名称 <NSObject>

  //方法申明列表。。。

@end

协议的遵守:

1》类遵守协议:

@interface 类名:父类名 <协议名称1,协议名称2>

@end

2》协议遵守协议

@protocol 协议名称 <其他协议名称1,其他协议名称2>

@end

3》协议中方法申明的关键字

1>@required(默认)

要求实现,如果没有实现,会发出警告

2>@optional

不要求实现,怎样都不会有警告

4》定义一个变量的时候,限制这个变量保存的对象遵守某个协议

类名<协议名称>*变量名;

id<协议名称>变量名;

NSObobject<MyProtocol>*obj;

id<MyProtocol>obj2;

如果没有遵守对应的协议,编译器会警告

5》@property中申明 的属性也可以做一个遵守的限制

@property(nanatomic,strong)类名<协议名称>*属性名;

@preperty(nanatomic,strong)id<协议名称>属性名;

18>foundation框架

NSString

创建字符串:

NSString *str = @“intheima”;

NSString *str2 = [[ NSString alloc]initWithString:@“23132”];

把c语言字符串转换成oc字符串

[[NSString alloc] initWithUTF8String:(const char *)];

 常用方法:

字符串中查找子串:NSRange range = [strangOfString:@:”https://”];

判断字符串的前缀:BOOL hasp= [strhasPrefix:@“http”];

等等。。。

NSMutableString

1.[NSMutableString stringWithFormat:@“jack”];

2.[[NSMutableString appendString:@“jabkc”]];

3.字符串删除[str deleteCharactersLnRange:NSMakeRange(1, 7)];

4.字符串替换:

[str replaceCharactersLnRange:range withString:@“ade”];

5.插入字符串:

[str insertString:@“sss”, atindex:6];

OC集合类1

1.NSArray,NSMuatlbeArray

2.NSSet, NSMutableSet

3.NSDictionary,NSMutableDictionary

NSArray

a.arrayWithObjects…

b.array objectAtlndex:i;

c.for(id obj in arryay){}

d.enumerateObjectsUsingBlock…

e.array containsObject:@“abc”

f.NSArray *array = [str componentsSeparateByString:@“-”];

g.[array componentsJoinedByString:@“-”];

h.arrayWithContentsOfFile…..

NSMutableArray

NSMutablArray *array = [NSMutablArray array];

集合的内存管理

1.添加一个元素就会对这个 元素做一次retain

2.删除一个元素就会对这个元素做一次release

3.当数组被销毁时,数组中的所有元素都会做一次release

array和set的区别:

1.array的元素是有序的。set中的元素是无序的

2.array允许存储的内容有相同的,set不允许存放相同的内容

NSDictionary:不允许有两个相同的key

允许有重复的value

NSNumber

将基本类型转换为对象

1.NSNumber *num1 = [NSNumber numberWithLnt:20];

2.int num = [num1 intValue];

3.NSNumber *num = @10;

OC新特性

1.NSNumber : @23

2.NSArray *array = @[@20,@“mike”,@2.3f];

3.NSDictionary

*dic = @{

@“”name”:@“mike”,@“address”:@“北京”

};

NSValue

NSNumber只能包装数值,不能用于封装结构体,NSValue用于封装结构体

1.转成对象

CGPoint p = CGPointMake(20, 20);

NSValue *value = [NSValue valueWithBytes:&p

objCType:@encode(CGPoint)];

2.转回结构体:

CGPoint p2;

[value getValue:&p2];

3系统自带的包装

[value pointValue];

NSDate

1.返回一个比现在晚1000

秒的时间

[NSDate dateWithTimeLntervalSinnceNow:1000];

2.随机返回一个将来的时间

NSDate *date = [NSDate distanFuture];

3.返回两个时间中比较晚多一个:

[dae earlierDate:date2];

4.返回两个时间中比较晚多一个

[datelaterDate:date2];

5.获取从1970到现在经过的毫秒数

[date timelntervalSince 1970];

6.获取当前时间

[NSDate date];

7.日期格式化

NSDateFormatter *format = [[NSDate Formatter] alloc]init]autorrelease];

format.dateFormat = @“yyyy=MM-dd HH:mm:ss”;

NSString *str = [format stringFromDate:date];

8.字符串转日期

NSDateFormatter *format = [[NSDateFormatter] alloc] init] autorelease];

format.dateFormat = @“yyyy-MM-dd HH:mm:ss”;

NSDate *date = [format dateFromString:@“2011-09-07 12:11:34”];

原文地址:https://www.cnblogs.com/coolcold/p/4674384.html