"IOS" uses Jsonmodel in Swift

Source: Internet
Author: User


Objective

First of all the model or the use of OC to write-see this sentence is not to close the page-#, in Swift directly write the error so will be used OC to write, here is mainly to share the experience with the use of Alamofire .


StatementWelcome reprint, but please keep the original source of the article:)Blog Park: http://www.cnblogs.comFarmer Uncle: Http://over140.cnblogs.com


Body

This is not a discussion of the two items of Jsonmodel and Alamofire, directly on the code

BaseModel.h

#import"JSONModel.h"

@interfaceBasemodel:jsonmodel

-(Instancetype) Initwithdictionary: (nsdictionary*) dict;

@end

basemodel.m

#import"BaseModel.h"

@implementationBasemodel

//Make all model properties Optional (avoid if possible)
+ (BOOL) propertyisoptional: (nsstring*) PropertyName
{
returnYES;
}

-(Instancetype) Initwithdictionary: (nsdictionary*) Dict {
return(self = [[super init] initwithdictionary:dict Error:nil]);
}

@end

All model has to inherit Basemodel, the other way of writing is the same

Baseapi.swift

InternalFunc requestmodel<t:basemodel> (Method:method, _ urlstring:urlstringconvertible, parameters: [String:anyobject ]? = Nil, Success: (T), void, failure: (nserror?), void) {
Mhttpmanager.request (method, URLString, Parameters:parameters, Encoding:ParameterEncoding.JSON)
. Responsejson {(Request, response, data, error)inch
ifError = = Nil {
ifLet Dict = Data as? nsdictionary {
ifLet model = T (dictionary:dict as[Nsobject:anyobject]) {
Success (model)
return
}
}
}
Failure (Error)
}
}

InternalFunc requestarray<t:basemodel> (Method:method, _ urlstring:urlstringconvertible, parameters: [String:anyobject ]? = Nil, Success: (array<t>), void, failure: (nserror?), void) {
Mhttpmanager.request (method, URLString, Parameters:parameters, Encoding:ParameterEncoding.JSON)
. Responsejson {(Request, response, data, error)inch
ifError = = Nil {
ifLet array = data as? Nsarray {
ifLet result = t.arrayofmodelsfromdictionaries (array as[Anyobject]). Copy () as? array<t>{
Success (Result)
return
}
}
}
Failure (Error)
}
}

Code Description

1, Mhttpmanager This is the Manager object of Alamofire

2, pay attention to the return data format of the service side, here support Model and array<model>

3, notice in Swift inside nsdictionary to model, with t (dictionary:dict as [Nsobject:anyobject]), this t is the specific generic type

4, note in Swift inside Nsarray to the model array, with t.arrayofmodelsfromdictionaries (array as [Anyobject]). Copy () as? Array<t>, take care not to use Basemodel. Arrayofmodelsfromdictionaries (compilation will not error but type cannot be turned out)

5. Specific usage:

PublicFunc Casts (Success: (array<custommodel>), void, failure: (nserror?), void) {
Requestarray (Method.get, url_casts, Parameters:nil, success:success, Failure:failure)
}

PublicFunc like (id:string, Success: (Custommodel), void, failure: (nserror?), void) {
Requestmodel (Method.patch, String (format:url_casts_like, id), Parameters:nil, success:success, Failure:failure)
}

Very easy and simple, you can write a lot less repetitive code.

End

will also share more swift experience, Welcome to exchange!

"IOS" uses Jsonmodel in Swift

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.