iOS   发布时间:2022-05-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 如何在块中获取变量的值大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

无论如何都要使用COR_243_11845@pletionHandler检查’type’变量的值. -(void)sendApiMethod:(NSString*)apiName ApiType:(NSString*)type { [SendAPI setAPIWithName:@"APIName" completionHandler:^(NSArray *errors) { if([type isE
无论如何都要使用COR_243_11845@pletionHandler检查’type’变量的值.

-(void)sendApiMethod:(NSString*)apiName ApiType:(NSString*)type
{
 [SendAPI setAPIWithName:@"APIName" completionHandler:^(NSArray *errors) {
        if([type isEqualToString:@"Login"])
        {
           /// Call Some Other function
        }
    }];
}

解决方法

我写了一小段代码来验证是否有效(只读你的问题,我会说是Droppy)

我在Simple View App的ViewController中添加了所有代码.
一些假设:
– 所有代码都是为了简洁….
– 我已经添加一个单例,因为它似乎正在调用一个方法.
– 实例方法有点粗糙,它只是保存名称和块
– 我为块添加一个typedef以便更好地阅读它.

#import "ViewController.h"



typedef void (^CompletionBlock)(NSArray *errors);


@interface SendAPI : NSObject

-(void)setAPIWithName:(NSString*)name completionHandler: (CompletionBlock)completionHandler;
+(void)setAPIWithName:(NSString*)name completionHandler: (CompletionBlock)completionHandler;
+(SendAPI*)sharedInstance;

@property (strong) CompletionBlock completionBlock;
@property (strong) NSString * name;

@end


@implementation SendAPI : NSObject

static SendAPI * _singleton = nil;



+(SendAPI*)sharedInstance
{
    if (_singleton == nil)
    {
        _singleton = [[SendAPI alloc] init];
    }
    return _singleton;
}


-(void)setAPIWithName:(NSString*)name completionHandler: (CompletionBlock)completionHandler;
{
    self.completionBlock = completionHandler;
    self.name = [name copy];

    __weak SendAPI * weakRef = self;


    dispatch_after(dispatch_time(DISPATCH_TIME_Now,(int64_t)(4 * NSEC_PER_SEC)),dispatch_get_main_queue(),^{

        NSError* err = [NSError errorWithDomain: @"delayed"
                                           code:1111
                                       userInfo: @{@"info": self.namE}
                        ];

        weakRef.completionBlock(@[err]);

    });

}


+(void)setAPIWithName:(NSString*)name completionHandler: (CompletionBlock)completionHandler;
{
    [[SendAPI sharedInstance]setAPIWithName:name completionHandler:completionHandler];

}


@end





@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self sendApiMethod:@"HelLO" ApiType: @"Login"];
}




-(void)sendApiMethod:(NSString*)apiName ApiType:(NSString*)type{

    [SendAPI setAPIWithName:@"APIName" completionHandler:^(NSArray *errors) {
     if([type isEqualToString:@"Login"])
     {
         /// Call Some Other function
         NSLog(@"%@",errors);
     }
 }];
}

它正确记录

@H_404_37@

大佬总结

以上是大佬教程为你收集整理的ios – 如何在块中获取变量的值全部内容,希望文章能够帮你解决ios – 如何在块中获取变量的值所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。