码迷,mamicode.com
首页 > 移动开发 > 详细

iOS OC10_Block

时间:2015-07-28 21:14:15      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:oc   区域   block   

内存五大区域

    //栈
    int a=10;
    NSLog(@"%p",a);
    //堆
    int *p=malloc(40);
    NSLog(@"%p",p);
    //全局
    static int b=10;
    NSLog(@"%p",b);
    //常量
    char *str="iphone";
    NSLog(@"%p",str);
    //代码区
   NSLog(@"%p",maxValue);

结果

2015-07-28 19:07:07.976 OC10_Block[829:71750] 0xa
2015-07-28 19:07:07.977 OC10_Block[829:71750] 0x1001068d0
2015-07-28 19:07:07.977 OC10_Block[829:71750] 0xa
2015-07-28 19:07:07.977 OC10_Block[829:71750] 0x100001f75

Block的四种形式

//1.无参数,无返回值
 void(^block)()=^(){
 //函数大括号里的代码段
 NSLog(@"今天有点多云");
 };
 //block的使用
 block();
 //block自己不会执行,必须调用才会执行block大括号里的代码段

结果

2015-07-28 19:25:43.452 OC10_Block[1239:82416] 今天有点多云
//2.有参数无返回值
void (^block)(int ,int)=^(int a,int b){
int max=a > b ? a : b;
NSLog(@"%d",max);
    };

//调用
block(10,20);
//void (^block)(int ,int)是block的类型,它的名是block,我们也通过名进行调用

结果

2015-07-28 19:27:08.121 OC10_Block[1249:82981] 20
//3.有返回值无参数
//有返回值在函数段的最后要加上return
int(^block)()=^(){

return 100;
};
//block调用时没有参数也要加上括号
NSLog(@"%d",block());

结果

2015-07-28 19:28:07.938 OC10_Block[1256:83327] 100
    //4.有参数有返回值
    NSString *(^block)(NSArray *,NSInteger)=^(NSArray *arr,NSInteger index){
        return arr[index];
    };
    NSLog(@"%@",block(@[@"1",@"2"],1));

结果

2015-07-28 19:29:05.096 OC10_Block[1263:84049] 2

练习

    //返回整数,参数字符串
    NSInteger (^block)(NSString *)=^(NSString *str){
        return str.integerValue;
    };
    NSLog(@"%ld",block(@"111"));

结果

2015-07-28 19:30:42.921 OC10_Block[1271:85319] 111
    //参数日期字符串,返回NSDate类型的日期对象
    NSDate *(^block)(NSString *)=^(NSString *str){
        NSDateFormatter *formatter=[[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy-MM-dd HH-mm-ss"];
        return [formatter dateFromString:str];
    };
    NSLog(@"%@",block(@"2015-07-28 10:12:12"));
    //block的函数段中更多使用参数,而返回值更多是调用一方使用

结果

2015-07-28 19:31:33.543 OC10_Block[1278:86117] 2015-07-28 02:12:12 +0000
//通过新的block写一个block
    Block block=^(NSArray *arr){
        return arr[0];
    };
    //block和局部变量
    //__block相当于给a一个在block中修改的权限,否则他只能取值,不能赋值
    __block int a=10;
    void(^block)()=^(){
        a=10*a;
        NSLog(@"%d",a);
    };
    block();

结果

2015-07-28 19:33:14.295 OC10_Block[1301:87551] 100
    //通过block对数组进行遍历
    NSArray *arr=[NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6", nil];
    //系统的block
    [arr enumerateObjectsUsingBlock:^(NSString * obj, NSUInteger idx, BOOL *stop) {
        NSLog(@"%@",obj);
    }];
    //系统的block的用法就是回车

结果

2015-07-28 19:33:52.913 OC10_Block[1308:88218] 1
2015-07-28 19:33:52.914 OC10_Block[1308:88218] 2
2015-07-28 19:33:52.914 OC10_Block[1308:88218] 3
2015-07-28 19:33:52.914 OC10_Block[1308:88218] 4
2015-07-28 19:33:52.915 OC10_Block[1308:88218] 5
2015-07-28 19:33:52.915 OC10_Block[1308:88218] 6
    //数组排序
    //数组对字符串的排序
    //不可变的数组
NSArray*arr=@[@"zhangyangyang",@"wuhe",@"liushansahn",@"wangjunmin",@"yanglin"];
 //SEL是方法选择器,他是方法选择器的类型
NSArray*newArr[arrsortedArrayUsingSelector:@selector(compare:)];
NSLog(@"%@",newArr);
NSLog(@"%@",arr);

结果

2015-07-28 19:35:02.000 OC10_Block[1319:89130] (
    zhangyangyang,
    wuhe,
    liushansahn,
    wangjunmin,
    yanglin
)
    //可变数组
NSMutableArray*arr[NSMutableArrayarrayWithObjects:@"zhangyangyang",@"wuhe",@"liushansahn",@"wangjunmin",@"yanglin", nil];
[arr sortUsingSelector:@selector(compare:)];
NSLog(@"%@",arr);

结果

2015-07-28 19:36:27.541 OC10_Block[1326:90109] (
    liushansahn,
    wangjunmin,
    wuhe,
    yanglin,
    zhangyangyang
)
    //数组对对象的排序
    //新建四个人
    Person *per1=[Person baseWithName:@"yanglin" sex:@"nan"];
    Person *per2=[Person baseWithName:@"wuhe" sex:@"nan"];
    Person *per3=[Person baseWithName:@"cunmou" sex:@"nv"];
    Person *per4=[Person baseWithName:@"liushanshan" sex:@"nan"];
    //用字面量创建对象和便利构造器是一样的
    NSArray *arr=@[per1,per2,per3,per4];

    //以学生的姓名作为排序的依据
NSArray*newArr[arrsortedArrayUsingComparator:^NSComparisonResult(Person * obj1, Person* obj2) {
      return -[obj1.name compare:obj2.name];
  }];
    for (Person *per in newArr) {
       NSLog(@"%@",per.name);
    }

结果

2015-07-28 19:37:53.065 OC10_Block[1340:91155] yanglin
2015-07-28 19:37:53.066 OC10_Block[1340:91155] wuhe
2015-07-28 19:37:53.066 OC10_Block[1340:91155] liushanshan
2015-07-28 19:37:53.066 OC10_Block[1340:91155] cunmou
    Person *per1=[Person baseWithName:@"yanglin" sex:@"nan"];
    Person *per2=[Person baseWithName:@"wuhe" sex:@"nan"];
    Person *per3=[Person baseWithName:@"cunmou" sex:@"nv"];
    Person *per4=[Person baseWithName:@"liushanshan" sex:@"nan"];
        NSMutableArray *marr=[NSMutableArray arrayWithObjects:per1,per2,per3,per4, nil];
    [marr sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        Person *per11=(Person *)obj1;
        Person *per22=(Person *)obj2;
        return [per11.name compare:per22.name];
    }];
    for (Person *per in marr) {
        NSLog(@"%@",per.name);
    }

结果

2015-07-28 19:38:59.851 OC10_Block[1347:91927] cunmou
2015-07-28 19:38:59.852 OC10_Block[1347:91927] liushanshan
2015-07-28 19:38:59.852 OC10_Block[1347:91927] wuhe
2015-07-28 19:38:59.852 OC10_Block[1347:91927] yanglin

补充

主函数之前

//    //C语言的函数
//    NSInteger maxValue(NSInteger a,NSInteger b){
//        return a > b ? a : b;
//    }


void test1()
{
    NSLog(@"今天真热");
}

int addNum(int a,int b){
    return a+b;
}

typedef int DT;
//新的类型写在*的后面,就是这里的PFUN
typedef int (*PFUN)(int ,int ) ;
typedef NSString *(^Block)(NSArray *) ;
//有返回值,无参数
typedef NSInteger(^TestBlock)();
//
//  BaseModel.h
//  OC10_Block
//
//  Created by dllo on 15/7/28.
//  Copyright (c) 2015年 zhozhicheng. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface BaseModel : NSObject

@property(nonatomic,copy)NSString *name;
@property(nonatomic,copy)NSString *sex;

-(id)initWithName:(NSString *)name
              sex:(NSString *)sex;


+(id)baseWithName:(NSString *)name
              sex:(NSString *)sex;




@end
//
//  BaseModel.m
//  OC10_Block
//
//  Created by dllo on 15/7/28.
//  Copyright (c) 2015年 zhozhicheng. All rights reserved.
//

#import "BaseModel.h"

@implementation BaseModel

-(id)initWithName:(NSString *)name
              sex:(NSString *)sex{
    self=[super init];
    if (self) {
        self.name=name;
        self.sex=sex;
    }return self;

}


+(id)baseWithName:(NSString *)name
              sex:(NSString *)sex{
    //多态方式完成
    id obj =[[[self class] alloc] initWithName:name sex:sex];
    return obj;
}

@end

版权声明:本文为博主原创文章,未经博主允许不得转载。

iOS OC10_Block

标签:oc   区域   block   

原文地址:http://blog.csdn.net/cheng_xiansheng/article/details/47109799

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!