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

Block学习一:基本用法,用block封装NSURLRequest异步请求

时间:2014-06-07 13:52:12      阅读:255      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   code   a   

首先看下面这段代码:

void (^myFirstBlock)(int theOne,int theTwo) = ^(int theOne,int theTwo){
        NSLog(@"====%d",theOne+theTwo);
    };
    myFirstBlock(100,300);

void:指的时返回数据类型,myFirstBlock:指的是block的名称,后面两个是参数。等号右边的block的实现。

个人理解:block本身就是一个类,他的实例化就是一个对象,下面看看这个数据请求的封装,来好好理解这句话。

我把数据请求这一款,封装在了:WebService这个类中,先看h文件:

<span style="font-size:14px;">//
//  WebService.h
//  BlockDemo
//
//  Created by junyuan ji on 14-6-3.
//  Copyright (c) 2014年 junyuan ji. All rights reserved.
//

#import <Foundation/Foundation.h>


typedef void (^successBlock)(NSMutableURLRequest * request,NSMutableData * reciveData);
typedef  void (^errorBlock)(NSMutableURLRequest * request);
typedef void (^startBlock)(NSMutableURLRequest * request);
typedef void (^complectionBlock)(NSMutableURLRequest * request,NSMutableData * reciveData);


@interface WebService : NSObject<NSURLConnectionDataDelegate,NSURLConnectionDelegate>
{
   __block NSHTTPURLResponse * httpResponse;
}
@property (nonatomic,copy) successBlock successblock;
@property (nonatomic,copy) errorBlock errorblock;
@property (nonatomic,copy) startBlock startblock;
@property (nonatomic,copy) complectionBlock complectionblock;
@property (nonatomic,retain) __block NSMutableData * reciveMutableData;

-(void)StartUrl:(NSString *)path;

-(void)Url:(NSString *)path onSuccess:(successBlock)successBlock onError:(errorBlock)errorBlock onStart:(startBlock)startBlock onCompletion:(complectionBlock)complectionBlock;
@end</span><span style="font-size:18px;">
</span>

上面typedef是声明block,下面把block声明称属性,比如successblock是一个对象,自然可以把它声明成属性。

下面看看m文件的代码:

//
//  WebService.m
//  BlockDemo
//
//  Created by junyuan ji on 14-6-3.
//  Copyright (c) 2014年 junyuan ji. All rights reserved.
//

#import "WebService.h"

@implementation WebService

-(void)Url:(NSString *)path onSuccess:(successBlock)successBlock onError:(errorBlock)errorBlock onStart:(startBlock)startBlock onCompletion:(complectionBlock)complectionBlock
{
    [self StartUrl:path];
    self.successblock = ^(NSMutableURLRequest * request,NSMutableData * reciveData){
        if (httpResponse.statusCode == 200)
        {
           successBlock(request,reciveData);
        }
    };
    self.startblock = ^(NSMutableURLRequest *request){
        startBlock(request);
    };
    self.errorblock = ^(NSMutableURLRequest * request){
        errorBlock(request);
    };
    self.complectionblock = ^(NSMutableURLRequest * request,NSMutableData * reciveData){
        complectionBlock(request,reciveData);
    };
}
-(void)StartUrl:(NSString *)path
{
    NSURL * url = [NSURL URLWithString:path];
    NSMutableURLRequest * request1 = [NSMutableURLRequest requestWithURL:url];
    [NSURLConnection connectionWithRequest:request1 delegate:self];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
     NSLog(@"%s",__FUNCTION__);
    self.errorblock((NSMutableURLRequest *)connection.currentRequest);
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSLog(@"%s",__FUNCTION__);
    httpResponse = (NSHTTPURLResponse *)response;
    self.reciveMutableData = [NSMutableData dataWithCapacity:0];
    self.startblock((NSMutableURLRequest *)connection.currentRequest);
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
     NSLog(@"%s",__FUNCTION__);
    [self.reciveMutableData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
     NSLog(@"%s",__FUNCTION__);
    self.complectionblock((NSMutableURLRequest *)connection.currentRequest,self.reciveMutableData);
}
@end

上面这几段代码,应该好好去理解,如果上面这段代码看懂了,block基本上也就掌握了。

下面看看如何使用封装的这个类:


#import "AppDelegate.h"
#import "WebService.h"

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    
    void (^myFirstBlock)(int theOne,int theTwo) = ^(int theOne,int theTwo){
        NSLog(@"====%d",theOne+theTwo);
    };
    myFirstBlock(100,300);
    
    
    [[[WebService alloc] init] Url:@"http://zhangmenshiting.baidu.com/data2/music/65532488/1490719597200128.mp3?xcode=f1ea4f162f276d435fc963a294370657c9d1e761819ce1d5" onSuccess:^(NSMutableURLRequest *request, NSMutableData *reciveData) {
        NSLog(@"reciveData = %@",reciveData);
    } onError:^(NSMutableURLRequest *request) {
        NSLog(@"error = ");
    } onStart:^(NSMutableURLRequest *request) {
        NSLog(@"start");
    } onCompletion:^(NSMutableURLRequest *request, NSMutableData *reciveData) {
        NSLog(@"completion");
    }];
    
    return YES;
}





Block学习一:基本用法,用block封装NSURLRequest异步请求,布布扣,bubuko.com

Block学习一:基本用法,用block封装NSURLRequest异步请求

标签:c   style   class   blog   code   a   

原文地址:http://blog.csdn.net/jijunyuan/article/details/28414031

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