iOS function tertiary encapsulation and block action __ function

Source: Internet
Author: User

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


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.