[IOS] Use JSONModel and iosswiftjsonmodel in Swift
Preface
First, all models are written using oc. If you see whether you want to close the webpage or not, you can directly write an error in swift, so you will use oc, here, I will share my experience with Alamofire.
StatementWelcome to repost, but please keep the original source of the article :) blog Park: http://www.cnblogs.com farmer UNCLE: http://over140.cnblogs.com
Body
Here we will not discuss the JSONModel and Alamofire projects.
BaseModel. h
# Import "JSONModel. h"
@ Interface BaseModel: JSONModel
-(Instancetype) initWithDictionary :( NSDictionary *) dict;
@ End
BaseModel. m
# Import "BaseModel. h"
@ Implementation BaseModel
// Make all model properties optional (avoid if possible)
+ (BOOL) propertyIsOptional :( NSString *) propertyName
{
Return YES;
}
-(Instancetype) initWithDictionary :( NSDictionary *) dict {
Return (self = [[super init] initWithDictionary: dict error: nil]);
}
@ End
All models must inherit the BaseModel, and all other writing methods are the same.
BaseAPI. swift
Internal func 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) in
If error = nil {
If let dict = data? NSDictionary {
If let model = T (dictionary: dict as [NSObject: AnyObject]) {
Success (model)
Return
}
}
}
Failure (error)
}
}
Internal func 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) in
If error = nil {
If let array = data? NSArray {
If let result = T. arrayOfModelsFromDictionaries (array as [AnyObject]). copy ()? Array <T> {
Success (result)
Return
}
}
}
Failure (error)
}
}
Code Description
1. mHttpManager: Alamofire Manager object
2. Pay attention to the data format returned by the server. Here Model and Array <Model> are supported.
3. Note that in Swift, NSDictionary is converted to Model and T (dictionary: dict as [NSObject: AnyObject]) is used. This T is a specific generic type.
4. Switch NSArray to the Model array in Swift and use T. arrayOfModelsFromDictionaries (array as [AnyObject]). copy ()? Array <T>. Be sure not to use BaseModel. arrayOfModelsFromDictionaries (compiling will not report an error, but the type cannot be converted)
5. Usage:
Public func casts (success: (Array <CustomModel>)-> Void, failure: (NSError ?) -> Void ){
RequestArray (Method. GET, URL_CASTS, parameters: nil, success: success, failure: failure)
}
Public func like (id: String, success: (CustomModel)-> Void, failure: (NSError ?) -> Void ){
RequestModel (Method. PATCH, String (format: URL_CASTS_LIKE, id), parameters: nil, success: success, failure: failure)
}
It is very easy and simple, and you can write less repetitive code.
End
More swift experiences will be shared in the future!