-(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 : NSObjectstatic 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); } }];}
它正确记录
总结以上是内存溢出为你收集整理的ios – 如何在块中获取变量的值全部内容,希望文章能够帮你解决ios – 如何在块中获取变量的值所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)