• 关于继承与类别的问题


    今天遇到一个问题,就是我在定义接口时

    首先父类

    #import "ApiNetWork.h"

    #define TimeOutSecs  30.0

    @implementation ApiNetWork

    +(id)instance

    {

        static dispatch_once_t onceToken = 0;

        __strong static ApiNetWork *instance = nil;

        

        _dispatch_once(&onceToken, ^{

            instance = [[self alloc] init];

        });

        

        return instance;

    }

    - (id)init {

        self = [super init];

        if (self) {

            _manager = [AFHTTPSessionManager manager];

            _manager.requestSerializer = [AFHTTPRequestSerializer serializer];

            _manager.responseSerializer = [AFHTTPResponseSerializer serializer];

            [_manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];

            _manager.requestSerializer.timeoutInterval = TimeOutSecs;// seconds of timeout

            [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];

        }

        return self;

    }

    - (void)httpGetFunc:(NSDictionary *)parameters

                    URL:(NSString *)url

                success:(void (^)(id result))success

                failure:(void (^)(NSError *error))failure;

    {

        [_manager GET:url parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {

        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

            success(responseObject);

        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

            failure(error);

        }];

    }

    - (void)httpPostFunc:(NSDictionary *)parameters

                     URL:(NSString *)url

                 success:(void (^)(id result))success

                 failure:(void (^)(NSError *error))failure

    {

        [_manager POST:url parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {

        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

           success(responseObject);

        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

           failure(error);

        }];

    }

    @end

    然后我又根据项目模块,分别定义了ApiNetWork+Home和ApiNetWork+appInit,然后分别将各自模块的接口写入对应的类中,然后发现问题了,就是接口出现了紊乱,因为父类使用了单例,然后子类只要调用,然后类名就一直是这个子类了,所以就导致其他子类接口错误,但是这个时候就可以换成类别。这样就不会出现问题了。只要在使用的类中,导入类别文件就可以了

    #import "ApiNetWork.h"

    //基础URL定义-------------------------------------------

    #define HOME_BANNER_URL            @"api/v2/cms/mobileBanners"

    #define HOME_INTRO_URL             @""

    #define HOME_INVESTMENT_URL        @""

    #define HOME_INVEST_URL            @""

    @interface ApiNetWork(Home)

    //完整URL(拼装 + 参数等)----------------------------------

    //首页banner信息

    -(NSString *)apiHomeBanner;

    //首页公告

    -(NSString *)apiHomeIntro;

    //首页用户资金信息

    -(NSString *)apiHomeInvestment;

    //首页理财列表

    -(NSString *)apiHomeInvest;

    //api网络请求方法------------------------------------------

    - (void)getHomeBanners:(NSDictionary *)parameters

                       URL:(NSString *)url

                   success:(void (^)(NSMutableArray *bannersMuArray))success

                   failure:(void (^)(NSError *error))failure;

    @end

    #import "ApiNetWork.h"

    #import "AcitivityImageModel.h"

    //基础URL定义-------------------------------------------

    #define ACITIVITY_IMAGE_URL  @"api/v2/cms/getAppopenimage"

    @interface ApiNetWork(AppInit)

    //完整URL(拼装 + 参数等)----------------------------------

    //启动页图片信息

    -(NSString *)apiAcitivityImage;

    //api网络请求方法------------------------------------------

    - (void)getAcitivityImage:(NSDictionary *)parameters

                          URL:(NSString *)url

                      success:(void (^)(AcitivityImageModel *responseObject))success

                      failure:(void (^)(NSError *error))failure;

    @end

  • 相关阅读:
    Andriod一段时间未操作页面,系统自动登出
    Error:Execution failed for task ':app:clean'
    Handler的postDelayed(Runnable, long)
    Android Studio快捷键大全
    Cookie、Session、Token的区别
    CentOS 7 上安装jdk
    CentOS 7 上搭建nginx来部署静态网页
    PyCharm如何设置 “ctrl+滚轮” 实现字体的放大和缩小
    HTTP和HTTPS
    性能测试思想(What is performance testing?)
  • 原文地址:https://www.cnblogs.com/huiyi-520/p/7374909.html
Copyright © 2020-2023  润新知