码迷,mamicode.com
首页 > 其他好文 > 详细

LessonBlock

时间:2015-03-10 21:24:15      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:

//

//  main.m

//  2-7 LessonBlock

//

//  Created by lanouhn on 15/3/9.

//  Copyright (c) 2015年 lanouhn. All rights reserved.

//

 

#import <Foundation/Foundation.h>

#import "Student.h"

#import "LoginManager.h"

//函数声明

int maxValue(int a, int b);

//函数定义

int maxValue(int a, int b) {

    return  a > b ? a : b;

}

 

//函数指针类型

//int (*)(int, int)

//函数指针变量

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

//函数指针类型冲命名

typedef int (*Type)(int, int);

//函数指针变量

Type p1 = NULL;

 int main(int argc, const char * argv[]) {

    @autoreleasepool {

        //函数调用

        int max = maxValue(1, 2);

        printf("%d\n", max);

        

        //函数指针指向函数首地址

        p =maxValue;

        //如果函数指针指向了函数的首地址, 这个指针就可以用来调用函数

        max = p(1, 2);

        printf("%d\n", max);

        

        //Block

        //Block的类型

        //返回值类型 (^)(参数类型1 参数名1, 参数类型2 参数名2, ...)

        //注:1.如果没有参数, 括号不能省略

        //   2.参数名一般省略, 特殊工艺情况为了使用方便, 参数名不省略

        //int (^)(int, int)

        

        //Block的定义

        //注:block的名字写在^后面

        int (^blockName)(int, int);

        

        //Block赋值(block的实现)

        //^(参数类型 参数名1, 参数类型2 参数名2, ...) {

        //      执行代码

        //}

        //注:1.参数名不能省略

        //   2.如果block有返回值,用return返回

        

        blockName = ^(int a, int b) {

            return a + b;

        };

        

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

            return a + b;

        };

        

        //Block的调用

        //block名(实参1, 实参2, ...)

        int sum = blockName(1, 2);

        NSLog(@"%d", sum);

        NSLog(@"%d", blockName1(4, 5));

        

        //写一个block, 用于实现答应Hello World;

        void (^hello)();

        hello = ^() {

            NSLog(@"Hello World");

        };

        hello();

        

        //typedef重命名block类型

        typedef void (^BlockType)();

        BlockType hello1 = ^() {

            NSLog(@"Hello World");

        };

        hello1();

        

        //写一个block, 用于实现打印Hello + 人名

        void (^hello2)(NSString *str);

        hello2 = ^(NSString *str) {

            NSLog(@"hello %@", str);

        };

        hello2(@"xiaoming");

 

        //写一个返回值为整型 参数为OC字符串 (仅一个参数)的block, 实现将字符串转换为整型的功能

        int (^change)(NSString *str1) = ^(NSString *str1) {

            return [str1 intValue];

        };

        NSString *string = @"2125";

        NSLog(@"%d", change(string));

    

        //block中变量的使用

        //1.block内部不能够访问局部变量, 需要加修饰词__block, __weak

        //2.block内部可以访问全局变量

        //3.ARC模式下用__weak, 基本数据类型用__block

        //4.MRC模式下全部用__block

        

        __block int number = 100;

        //block, 每次执行block, 修改number的值,并打印

        void (^addNumber)() = ^() {

            number = number +1;

            NSLog(@"%d", number);

        };

        addNumber();

        

        //block与数组排序

        NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:@"1023", @"562", @"485", @"785", @"111", nil];

        //按字符串顺序排序

        NSArray *sortArray1 = [array sortedArrayUsingSelector:@selector(compare:)];

        NSLog(@"%@", sortArray1);

        

        //NSOrderedDescending

        //NSOrderedSame

        //NSOrderedAscending

        

        

        NSArray *sortArray = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {

            //升序排序

            if ([obj1 intValue] > [obj2 intValue]) {

                return NSOrderedDescending; //前一个大

            } else if([obj1 intValue] < [obj2 intValue]) {

                return NSOrderedAscending;//后一个大

            } else {

                return NSOrderedSame;

            }

        }];

        NSLog(@"%@", sortArray);

        

        //用block对数组进行降序排序

        NSArray *array1 = @[@1.2, @45.6, @45.0, @22.1, @7.10];

        NSArray *sortArray3 = [array1 sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {

            if ([obj1 floatValue] > [obj2 floatValue]) {

                return NSOrderedDescending;

            } else if ([obj1 floatValue] < [obj2 floatValue]) {

                return NSOrderedAscending;

            } else {

                return NSOrderedSame;

            }

        }];

        NSLog(@"%@", sortArray3);

        

        Student *stu1 = [[Student alloc] initWithName:@"zijian" score:88.5];

        Student *stu2 = [[Student alloc] initWithName:@"junfei" score:99.0];

        Student *stu3 = [[Student alloc] initWithName:@"chengzhen" score:100.0];

        Student *stu4 = [[Student alloc] initWithName:@"gaoshang" score:80.0];

        

        //学生以姓名排序a - z

        NSMutableArray *mArray = [[NSMutableArray alloc] initWithCapacity:0];

        [mArray addObject:stu1];

        [mArray addObject:stu2];

        [mArray addObject:stu3];

        [mArray addObject:stu4];

        NSArray *sortArray4 = [mArray sortedArrayUsingComparator:^NSComparisonResult(Student *obj1, Student *obj2) {

//            return [obj1.name compare:obj2.name];

            if ([obj1.name compare:obj2.name] == 1) {

                return NSOrderedDescending;

            } else if ([obj1.name compare:obj2.name] == -1) {

                return NSOrderedAscending;

            } else {

                return NSOrderedSame;

            }

        }];

        NSLog(@"%@", sortArray4);

        

        //学生以分数由高到低排序

        NSArray *sortArray5 = [mArray sortedArrayUsingComparator:^NSComparisonResult(Student *obj1, Student *obj2) {

            if (obj1.score > obj2.score) {

                return NSOrderedDescending;

            } else if (obj1.score < obj2.score) {

                return NSOrderedAscending;

            } else {

                return NSOrderedSame;

            }

        }];

        NSLog(@"%@", sortArray5);

        

        //

        LoginManager *loginManager = [[LoginManager alloc] init];

        loginManager.account = @"Kris";

        loginManager.passWord = @"88888888";

        [loginManager login];

        

//        [loginManager loginWithSuccess:^(LoginManager *loginManager){

//            NSLog(@"我很高兴!");

//        } failure:^(LoginManager *loginManager){

//            NSLog(@"我不高兴!");

//        }];

//        

        

        [loginManager loginWithSuccess:^(LoginManager *loginManager){

            NSLog(@"%@",loginManager.account);

        } failure:^(LoginManager *loginManager){

            NSLog(@"%@", loginManager.passWord);

        }];

        

        //字面量, 语法糖

        NSString *string5 = @"ios";

        NSNumber *number1 = @100;

        NSArray *array7 = @[@"123", @"abc"];

        [array7 objectAtIndex:0];

        NSLog(@"%@", array7[1]);

        NSDictionary *dic = @{@"name": @"zhangsan", @"age": @12};

        NSLog(@"%@", dic[@"name"]);

}

    return 0;

}

 

LessonBlock

标签:

原文地址:http://www.cnblogs.com/xiaoxuetongxie/p/4328287.html

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