iOS 的块操作也可以象Java匿名类一样到处都是

iOS 的块操作也可以象Java匿名类一样到处都是

太阳火神的美丽人生 (http://blog.csdn.net/opengl_es)

本文遵循“署名-非商业用途-保持一致”创作公用协议

转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS、Android、Html5、Arduino、pcDuino否则,出自本博客的文章拒绝转载或再转载,谢谢合作。



整理一份之前对 ASIHTTPRequest 的分类封装

今天有点累,突然想到,先留个坑,明天用足足的代码来填满这个坑 ,后续会把以前工作中做的总结,陆续回忆并记录到这里。

预告:

NSData+ASIHTTPRequest

NSDictionary+ASIHTTPRequest

NSObject+ASIHTTPRequest

NSString+ASIHTTPRequest


下面开始填坑,以下仅对 NSObject的分类给予相关代码说明,其它的可以从这个扩展,就自个弄吧。

类声明文件:

//NSObject+ASIHTTPRequest.h
#import <Foundation/Foundation.h>
#import "ASIFormDataRequest.h"

@interface NSObject (ASIHTTPRequest)

- (void)postUrlPath:(NSString *)urlPath body:(id)body success:(void (^)(ASIFormDataRequest *request))success fail:(void (^)(ASIFormDataRequest *request))fail;
- (void)getUrlPath:(NSString *)urlPath param:(id)param success:(void (^)(ASIFormDataRequest *request))success fail:(void (^)(ASIFormDataRequest *request))fail;
- (void)requestUrlPath:(NSString *)urlPath method:(NSString *)method body:(id)body success:(void (^)(ASIFormDataRequest *request))success fail:(void (^)(ASIFormDataRequest *request))fail;

@end

类实现文件:

// NSObject+ASIHTTPRequest.m

#import "NSObject+ASIHTTPRequest.h"

@implementation NSObject (ASIHTTPRequest)


- (void)postUrlPath:(NSString *)urlPath body:(id)body success:(void (^)(ASIFormDataRequest *request))success fail:(void (^)(ASIFormDataRequest *request))fail {
    
    [self requestUrlPath:urlPath method:@"POST" body:body success:success fail:fail];
}

- (void)getUrlPath:(NSString *)urlPath param:(id)param success:(void (^)(ASIFormDataRequest *request))success fail:(void (^)(ASIFormDataRequest *request))fail {
    
    [self requestUrlPath:urlPath method:@"GET" body:param success:success fail:fail];
}

- (void)requestUrlPath:(NSString *)urlPath method:(NSString *)method body:(id)body success:(void (^)(ASIFormDataRequest *request))success fail:(void (^)(ASIFormDataRequest *request))fail {
    
    NSURL *url = [NSURL URLWithString:urlPath];
    __block ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    __weak ASIFormDataRequest *blockRequest = request;
    
    [request setShouldContinueWhenAppEntersBackground:YES];
    [request setPostValue:[UIDevice currentDevice].model forKey:@"clienttype"];
    [request setRequestMethod:method];
    
    // 如果是 GET 或 POST 方法,并且协议体不为空,则添加参数(以下更适用于 ASIHTTPRequest)
    if (nil != body) {
        
        if ([@"POST" isEqualToString:method]) {
            
            if ([body isKindOfClass:[NSDictionary class]]) {
                
                NSDictionary *bodyDic = (NSDictionary *)body;
                for (NSString *key in bodyDic.allKeys) {
                    
                    id object =[bodyDic objectForKey:key];
                    [request setPostValue:object forKey:key];
                }
            }
            else if ([body isKindOfClass:[NSString class]] || [body isKindOfClass:[NSData class]]) {
                
                NSData *tempData = nil;
                if ([body isKindOfClass:[NSString class]]) {
                    tempData = [body dataUsingEncoding:NSUTF8StringEncoding];
                }
                else {
                    tempData = body;
                }
                [request setPostBody:[NSMutableData dataWithData:tempData]];
                [request setPostLength:[tempData length]];
            }
            
        }
        else if ([@"GET" isEqualToString:method]) {
            
            // 增加 GET 参数
            if ([body isKindOfClass:[NSDictionary class]]) {
                
                NSDictionary *bodyDic = (NSDictionary *)body;
                NSMutableString *paramStr = [[NSMutableString alloc] init];
                for (NSString *key in bodyDic.allKeys) {
                    
                    id object =[bodyDic objectForKey:key];
                    [paramStr appendFormat:@"%@=%@&", key, object];
                }
                NSString *urlWithParam = [urlPath stringByAppendingFormat:@"?%@", paramStr];
                NSLog(@"urlWithParam = %@", urlWithParam);
                request.url = [NSURL URLWithString:urlWithParam];
            }
            else if ([body isKindOfClass:[NSString class]] || [body isKindOfClass:[NSData class]]) {
                
                NSString *paramStr = nil;
                if ([body isKindOfClass:[NSString class]]) {
                    paramStr = (NSString *)body;
                }
                else {
                    paramStr = [[NSString alloc] initWithData:body encoding:NSUTF8StringEncoding];
                }
                NSString *urlWithParam = [urlPath stringByAppendingFormat:@"?%@", paramStr];
                NSLog(@"urlWithParam = %@", urlWithParam);
                request.url = [NSURL URLWithString:urlWithParam];
            }
            
            // 清空 POST 包体
            [request setPostBody:nil];
            [request setPostLength:0];
        }
        else {
            
        }
    }
    
    [request setCompletionBlock:^{
        
        success(blockRequest);
    }];
    
    [request setFailedBlock:^{
        fail(blockRequest);
    }];
    
    [request startAsynchronous];
}

@end


上一篇:二叉树的递归和非递归遍历


下一篇:MongoDB创建一个用户自定义角色