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