在Swift中使用JSONModel 實(shí)例代碼
前言
首先所有的Model還是使用oc來(lái)寫(xiě)——看到這一句是不是想關(guān)網(wǎng)頁(yè)了- - #,在swift里面直接寫(xiě)一直報(bào)錯(cuò)所以就將就用oc來(lái)寫(xiě)了,這里主要是分享一下搭配Alamofire使用的經(jīng)驗(yàn)。
正文
這里不討論JSONModel和Alamofire這兩個(gè)項(xiàng)目,直接上代碼,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,其他寫(xiě)法都一樣
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)
}
}
代碼說(shuō)明
1、mHttpManager這個(gè)是Alamofire的Manager對(duì)象
2、注意服務(wù)端的返回的數(shù)據(jù)格式,這里支持Model和Array<Model>
3、注意在Swift里面NSDictionary轉(zhuǎn)Model,用T(dictionary: dict as [NSObject : AnyObject]),這個(gè)T就是具體的泛型類(lèi)型
4、注意在Swift里面NSArray轉(zhuǎn)Model數(shù)組,用T.arrayOfModelsFromDictionaries(array as [AnyObject]).copy() as? Array<T>,注意不要用BaseModel. arrayOfModelsFromDictionaries(編譯不會(huì)報(bào)錯(cuò)但是類(lèi)型轉(zhuǎn)不出來(lái))
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 實(shí)例代碼,有需要的朋友可以參考下。
相關(guān)文章
IOS Xib控件拖拽與頁(yè)面跳轉(zhuǎn)實(shí)例
下面小編就為大家分享一篇IOS Xib控件拖拽與頁(yè)面跳轉(zhuǎn)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
iOS開(kāi)發(fā)KVO實(shí)現(xiàn)細(xì)節(jié)解密
這篇文章主要為大家介紹了iOS開(kāi)發(fā)KVO實(shí)現(xiàn)細(xì)節(jié)解密,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
手把手教你實(shí)現(xiàn)微信小視頻iOS代碼實(shí)現(xiàn)
這篇文章主要手把手教你實(shí)現(xiàn)微信小視頻,iOS代碼實(shí)現(xiàn)微信小視頻功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08
Framework中實(shí)現(xiàn)OC和Swift的混編方案
這篇文章主要為大家介紹了Framework中實(shí)現(xiàn)OC和Swift的混編方案詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
iOS 封裝導(dǎo)航欄及返回,獲取控件所在控制器的實(shí)例
下面小編就為大家分享一篇iOS 封裝導(dǎo)航欄及返回,獲取控件所在控制器的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
iOS中UIScrollView嵌套UITableView的實(shí)踐教程
在UIScrollView嵌套UITableView的問(wèn)題相信大家都遇到過(guò),小編最近在工作中就遇到了這個(gè)問(wèn)題,所以這篇文章主要介紹了iOS中UIScrollView嵌套UITableView的相關(guān)資料,文中介紹的方法是通過(guò)自己的實(shí)踐所得來(lái)的,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-05-05

