在Swift中使用JSONModel 執行個體代碼_IOS

來源:互聯網
上載者:User

前言

 首先所有的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 執行個體代碼,有需要的朋友可以參考下。

相關文章

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.