iOS function tertiary encapsulation and block function
In iOS, two times, three encapsulation of commonly used modules or public modules, two encapsulation of known APIs to make it easier to use in your project.
Here is an example:
+ (Nsurlsessiondatatask *) Requestdictionary: (void (^) (nsdictionary *, Nserror *)) block
Withurl: (nsstring *) URL
Parameter: (nsdictionary *) parameter
{
NSLog (@ "url====%@", url);
return [[Scappapiclient SessionManager] Get:url
Parameters:parameter
success:^ (nsurlsessiondatatask *task, id responseobject) {
NSString *successstring = [[Nsjsonserialization jsonobjectwithdata:responseobject
Options:kniloptions
Error:null] objectforkey:@ "Value"];
Nsdictionary *resultdict;
if ([successstring isequaltostring:@ "Success"]) {
Resultdict = [[[Nsjsonserialization Jsonobjectwithdata:responseobject
Options:kniloptions
Error:null] valueforkeypath:@ "Successinfo"] objectatindex:0];
}
if ([Successstring isequaltostring:@ "failed"]) {
Resultdict = [Nsjsonserialization jsonobjectwithdata:responseobject
Options:kniloptions
Error:null];
}
if (block) {
Block ([Nsdictionary dictionarywithdictionary:resultdict], nil);
}
} failure:^ (Nsurlsessiondatatask *task, Nserror *error) {
if (block) {
Block ([Nsdictionary dictionary], error);
}
}];
}
Scappapiclient SessionManager
Gets the return value of a simple, and a method behind the get is the API self-band method;
Simple for
#import "SCAppAPIClient.h"
#import "Macros.h"
@implementation Scappapiclient
+ (Instancetype) SessionManager
{
static scappapiclient *_sessionmanager = nil;
Static dispatch_once_t Oncetoken;
Dispatch_once (&oncetoken, ^{
_sessionmanager = [[Self alloc] initwithbaseurl:[nsurl Urlwithstring:official_url]];
_sessionmanager.requestserializer = [Afjsonrequestserializer serializer];
_sessionmanager.responseserializer = [Afhttpresponseserializer serializer];
});
return _sessionmanager;
}
@end