【问题标题】:objective c completion handler目标c完成处理程序
【发布时间】:2017-03-04 12:32:02
【问题描述】:

我有带有 getContentInBackgroundWithMemberId 和 postRequest 函数的 RequestManager 类。我想从我的视图控制器调用它们并使用完成处理程序获取结果。如何编辑我的函数?

RequestManager.h

#import <Foundation/Foundation.h>

@interface RequestManager : NSObject

-(void)getContentInBackgroundWithMemberId:(int)memberId;

@end

RequestManager.m

#import "RequestManager.h"

@implementation RequestManager


-(void)postRequestWithParams:(NSDictionary*)params
{
NSString *parameters = @"encrypt=93mrLIMApU1lNM619WzZje4S9EeI4L2L";
for(id key in params)
{
   NSString *obj = [NSString stringWithFormat:@"&%@=%@",key,[params objectForKey:key]];
   [parameters stringByAppendingString:obj];
}

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://someserver"]];

NSData *postBody = [parameters dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postBody];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
    if(!connectionError)
    {
        NSDictionary *serverData =[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        NSArray *result = [NSArray array];
        result = [serverData objectForKey:@"result"];
    }

}];
}


-(void)getContentInBackgroundWithMemberId:(int)memberId
{
NSDictionary *params = [NSDictionary dictionary];
params = @{@"member_id":[NSNumber numberWithInt:memberId]};

[self postRequestWithParams:params];
}

@end

ViewController.h

#import <UIKit/UIKit.h>
#import "RequestManager.h"

@interface ViewController : UIViewController

@property (strong,nonatomic) RequestManager *requestManager;

@end

ViewController.m

#import "ViewController.h"

@interface ViewController

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

int memberId = 82;

//here i want to call getContentInBackgroundWithMemberId and get result using completion handler;
_requestManager = [[RequestManager alloc]init];
[_requestManager getContentInBackgroundWithMemberId:memberId];

}

@end

【问题讨论】:

    标签: objective-c completionhandler


    【解决方案1】:

    RequestManager.h

    @interface RequestManager : NSObject
        //Create a block property
        typedef void(^postRequestBlock)(BOOL status);
    
        -(void) getContentInBackgroundWithMemberId: (int) memberId completed:(postRequestBlock)completed;
    @end
    

    RequestManager.m

    -(void) getContentInBackgroundWithMemberId: (int) memberId completed:(postRequestBlock)completed{
        NSDictionary * params = [NSDictionary dictionary];
        params = @ {
            @ "member_id": [NSNumber numberWithInt: memberId]
        };
        [self postRequestWithParams: params completed:^(BOOL status){
            completed(status);
        }];
    }
    
    //Add completion block.
    -(void) postRequestWithParams: (NSDictionary * ) params completed:(postRequestBlock)completed{
        [NSURLConnection sendAsynchronousRequest: request queue: [NSOperationQueue mainQueue] completionHandler: ^ (NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
            if (!connectionError) {
                NSDictionary * serverData = [NSJSONSerialization JSONObjectWithData: data options: 0 error: nil];
                NSArray * result = [NSArray array];
                result = [serverData objectForKey: @ "result"];
                completed(YES);
            } else {
                completed(NO);
            }
    
        }];
    }
    

    已经有大量关于区块的问答。这可能有助于进一步解释 How to write an Objective-C Completion Block

    【讨论】:

    • 谢谢。当我从视图控制器调用它时是什么语法?
    • @DenisWindover getContentInBackgroundWithMemberId: (int) memberId completed:^(BOOL status){ //called after request has been completed.}
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-03
    • 2015-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多