#import "JSONModel.h"
@interface BaseModel : JSONModel
-(instancetype)initWithDictionary:(NSDictionary*)dict;
@end
#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
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)
}
}
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)
}