在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
前言 首先所有的Model还是使用oc来写——看到这一句是不是想关网页了- - #,在swift里面直接写一直报错所以就将就用oc来写了,这里主要是分享一下搭配Alamofire使用的经验。 声明
欢迎转载,但请保留文章原始出处:)
博客园:http://www.cnblogs.com
农民伯伯: http://over140.cnblogs.com
正文 这里不讨论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) } 非常轻松和简单, 可以少写很多重复代码。
后期维护 2015-05-20 Alamofire兼容iOS7有点问题,设置head不管用,请参考我另外一篇文章:【iOS】Alamofire库在iOS7下设置Head无效的问题
2016-04-21 错误:fatal error: NSArray element failed to match the Swift Array Element type,参考帖子这里。 Swift 中使用不支持嵌套 JSONModel 数组
@property (strong, nonatomic) NSArray<App *> *apps; @property (strong, nonatomic) NSArray<User *> *users; 解决办法:
@property (strong, nonatomic) NSArray<NSDictionary *> *apps; @property (strong, nonatomic) NSArray<NSDictionary *> *users; 然后在 Swift 这边转一下 self.users = users.map{ User(dictionary: $0 ) } self.apps = apps.map{ App(dictionary: $0 ) }
结束 以后还会分享更多swift的经验,欢迎交流! |
请发表评论