Use the JSONModel instance code in Swift

  • 2020-06-15 10:20:46
  • OfStack

preface

First of all, all Model is still written in oc. In swift, it is written in oc, so it will be written in oc. Here is mainly to share the experience of using 1 with Alamofire.

The body of the

JSONModel and Alamofire are not discussed here. The code is 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 Model are inherited from BaseModel, otherwise written the same way

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 as? 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 as? NSArray {
            if let result = T.arrayOfModelsFromDictionaries(array as [AnyObject]).copy() as? Array<T>{
              success(result)
              return
            }
          }
        }
        failure(error)
    }
  }

Code instructions

This is the Manager object of Alamofire

2. Note the data format returned by the server. Model and Array are supported here < Model >

3. Note that NSDictionary turns Model in Swift, using T(dictionary: dict as [NSObject: AnyObject]). This T is the specific generic type

4, note in Swift NSArray Model array, using T.arrayOfModelsFromDictionaries(array as [AnyObject]).copy() as? Array < T > , BaseModel. arrayOfModelsFromDictionaries (compiler will not report errors but type cannot be converted)

5. Specific 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)
      }

Above is to use JSONModel instance code in Swift, friends in need can refer to below.


Related articles: