关于继承与类别的问题
Posted 回忆12
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于继承与类别的问题相关的知识,希望对你有一定的参考价值。
今天遇到一个问题,就是我在定义接口时
首先父类
#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
以上是关于关于继承与类别的问题的主要内容,如果未能解决你的问题,请参考以下文章