RQBaseModel.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. //
  2. // RQBaseModel.h
  3. // RQCommon
  4. //
  5. // Created by 张嵘 on 2018/11/14.
  6. // Copyright © 2018 张嵘. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. #import <YYModel/YYModel.h>
  10. @interface RQBaseModel : NSObject <YYModel,NSCopying,NSCoding>
  11. /// YYModel - API
  12. /// 将 Json (NSData,NSString,NSDictionary) 转换为 Model
  13. + (instancetype)modelWithJSON:(id)json;
  14. /// 字典转模型
  15. + (instancetype)modelWithDictionary:(NSDictionary *)dictionary;
  16. /// json-array 转换为 模型数组
  17. + (NSArray *)modelArrayWithJSON:(id)json;
  18. /// 将 Model 转换为 JSON 对象
  19. - (id)toJSONObject;
  20. /// 将 Model 转换为 NSData
  21. - (NSData *)toJSONData;
  22. /// 将 Model 转换为 JSONString
  23. - (NSString *)toJSONString;
  24. // Returns the keys for all @property declarations, except for `readonly`
  25. // properties without ivars, or properties on RQBaseModel itself.
  26. /// 返回所有@property声明的属性,除了只读属性,以及属性列表不包括成员变量
  27. + (NSSet *)propertyKeys;
  28. // A dictionary representing the properties of the receiver.
  29. //
  30. // The default implementation combines the values corresponding to all
  31. // +propertyKeys into a dictionary, with any nil values represented by NSNull.
  32. // This property must never be nil.
  33. @property (nonatomic, copy, readonly) NSDictionary *dictionaryValue;
  34. @property (nonatomic, assign, readwrite) NSInteger total;
  35. // Merges the value of the given key on the receiver with the value of the same
  36. // key from the given model object, giving precedence to the other model object.
  37. //
  38. // By default, this method looks for a `-merge<Key>FromModel:` method on the
  39. // receiver, and invokes it if found. If not found, and `model` is not nil, the
  40. // value for the given key is taken from `model`.
  41. - (void)mergeValueForKey:(NSString *)key fromModel:(RQBaseModel *)model;
  42. // Merges the values of the given model object into the receiver, using
  43. // -mergeValueForKey:fromModel: for each key in +propertyKeys.
  44. //
  45. // `model` must be an instance of the receiver's class or a subclass thereof.
  46. - (void)mergeValuesForKeysFromModel:(RQBaseModel *)model;
  47. @end
  48. // Implements validation logic for RQBaseModel.
  49. @interface RQBaseModel (Validation)
  50. // Validates the model.
  51. //
  52. // The default implementation simply invokes -validateValue:forKey:error: with
  53. // all +propertyKeys and their current value. If -validateValue:forKey:error:
  54. // returns a new value, the property is set to that new value.
  55. //
  56. // error - If not NULL, this may be set to any error that occurs during
  57. // validation
  58. //
  59. // Returns YES if the model is valid, or NO if the validation failed.
  60. - (BOOL)validate:(NSError **)error;
  61. @end