在Swift中使用JSONModel 实例代码

前言

 首先所有的Model还是使用oc来写——看到这一句是不是想关网页了- - #,在swift里面直接写一直报错所以就将就用oc来写了,这里主要是分享一下搭配Alamofire使用的经验。

正文

 这里不讨论JSONModel和Alamofire这两个项目,直接上代码,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

 所有的Model都要继承BaseModel,其他写法都一样

  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)
    }
  }

 代码说明

   1、mHttpManager这个是Alamofire的Manager对象

   2、注意服务端的返回的数据格式,这里支持Model和Array<Model>

   3、注意在Swift里面NSDictionary转Model,用T(dictionary: dict as [NSObject : AnyObject]),这个T就是具体的泛型类型

   4、注意在Swift里面NSArray转Model数组,用T.arrayOfModelsFromDictionaries(array as [AnyObject]).copy() as? Array<T>,注意不要用BaseModel. arrayOfModelsFromDictionaries(编译不会报错但是类型转不出来)

   5、具体用法:

 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)
      }

以上就是在Swift中使用JSONModel 实例代码,有需要的朋友可以参考下。