Swift4 调用 OC 写的组件 - delegate篇

首先展示一个请求API的组件:

EntAPI.h

#import <Foundation/Foundation.h>

#import "QSRequest.h"

typedef NS_ENUM(NSUInteger, EntAPIName) {
    EntAPINameEcapi_article_list,
};

@protocol EntAPIDelegate;

@interface EntAPI : NSObject

/*****************************************/

@property(nonatomic,weak,nullable) id<EntAPIDelegate> delegate;

+ (instancetype _Nonnull ) apiWithDelegate:(_Nonnull id<EntAPIDelegate>)delegate;

/*****************************************/

- (void) APIByEcapi_article_list;

@end

@protocol EntAPIDelegate <NSObject>

@optional
- (void) entAPI:(nonnull EntAPI*)aAPI withName:(EntAPIName)name withResult:(nullable QSRequestResult*)result withError:(nullable NSError*)error;

@end

Swift 的调用:

import UIKit

class EntIndexController: UIViewController,EntAPIDelegate {
    
    var theAPI : EntAPI? = nil

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        self.view.backgroundColor = UIColor.red
        
        theAPI = EntAPI.init(delegate: self)
        theAPI?.apiByEcapi_article_list()
        
    }
    
    func entAPI(_ aAPI: EntAPI, with name: EntAPIName, with result: QSRequestResult?, withError error: Error?) {
        print(result!.json!)
    }
}