首页 > 代码库 > block练习题

block练习题

//统计字符串字符个数

NSString *str=@"*******";

        NSLog(@"str 的长度为: %i",(int)[str length]);

 

 

//判断用户名与密码是否相同,如果相同,输出right

#import <Foundation/Foundation.h>

 

@interface UserService : NSObject

 

-(void)sayHello:(NSString *)hello withIsMember:(BOOL(^)(NSString *userName,NSString *password))judge;

@end

 

#import "UserService.h"

 

@implementation UserService

-(void)sayHello:(NSString *)hello withIsMember:(BOOL (^)(NSString *, NSString *))judge

{

    if (judge(@"Jereh",@"Jereh")) {

        NSLog(@"%@",hello);

    }

    else

    {

        NSLog(@"NO");

    }

}

@end

 

 

#import <Foundation/Foundation.h>

#import "UserService.h"

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

{

 

    @autoreleasepool {

        

        UserService *theUser=[[UserService allocinit];

        [theUser sayHello:@"Right" withIsMember:^(NSString *userName,NSString *password){

            if ([userName isEqualToString: password]) {

                return YES;

            }

            else

                return NO;

        }];

}

    return 0;

}

block练习题