Swift4 调用 OC 写的组件

文中采用到的组件 QSRequest 是一个基于 NSURLSession 的封装。

Objectivate-C 的写法:

@interface ViewController ()

@end

@implementation ViewController
{
    BOOL actionAPI;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    QSRequest* request = [QSRequest new];
    request.target = self;
    request.URLString = @"http://api.XXX.com/v2/ecapi.article.list";
    [request setHTTPBody:@"14" forKey:@"id"];
    [request setHTTPBody:@"1" forKey:@"page"];
    [request setHTTPBody:@"5" forKey:@"per_page"];
    [request setupSuccessBlock:^(QSRequest *request, QSRequestResult *result) {
        NSLog(@"%@",result.json);
    } failureBlock:^(QSRequest *request, QSRequestResult *result) {
        
    }];
    [request PostRequestWithOnly:&actionAPI];
    
}



Swift 的写法:

class ViewController: UIViewController {
    var actionAPI: ObjCBool = ObjCBool(false)

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        let request = QSRequest()
        request.target = self
        request.urlString = "http://api.XXX.com/v2/ecapi.article.list"
        request.setHTTPBody("14", forKey: "id")
        request.setHTTPBody("1", forKey: "page")
        request.setHTTPBody("5", forKey: "per_page")
        request.setupSuccessBlock({ (request:QSRequest, result:QSRequestResult) in
            print(result.json!)
        }) { (request, result) in
            
        }
        request.post(withOnly: &actionAPI)
        
    }
}