zoukankan      html  css  js  c++  java
  • iOS中 block 做函数参数

    1.在.h中

    #import <Foundation/Foundation.h>
    
    //typedef void(^success)(id);
    
    @interface NetUtil : NSObject
    
    
    /** 通过session获取 POST网络请求 */
    
    
    + (void) POST:(NSURL *) url SuccessState:(void(^)(id responseData))success FailureState:(void(^)(id error))failure;
    
    @end

    2.在.m中

    #import "NetUtil.h"
    
    @implementation NetUtil
    
    #pragma mark - 通过NSURLSession进行POST请求
    + (void) POST:(NSURL *) url SuccessState:(void(^)(id responseData))success FailureState:(void(^)(id error))failure{
    
        NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL:url];
        
        request.HTTPMethod = @"POST";
        request.timeoutInterval = 6;
        
        NSURLSessionDataTask * dataTask = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            if (error != nil || data == nil || response == nil) {
                // 请求失败
                failure(error);
            }else{
                // 请求成功
                success(data);
            }
        }];
        [dataTask resume];
    }
    
    @end
  • 相关阅读:
    音频处理之回声消除及调试经验
    音频软件开发中的debug方法和工具
    ZJOI2015地震后的幻想乡
    HEOI2015小L的白日梦
    THUWC2017随机二分图
    PKUWC Slay The Spire
    dp的一些计划
    鸡汤征集贴
    弱菜的各种模板
    洛谷P4902乘积
  • 原文地址:https://www.cnblogs.com/code-Officer/p/6640529.html
Copyright © 2011-2022 走看看