[IOS] Use JSONModel and iosswiftjsonmodel in Swift

Source: Internet
Author: User

[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!

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.