RQBaseModel.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. // Merges the value of the given key on the receiver with the value of the same
  35. // key from the given model object, giving precedence to the other model object.
  36. //
  37. // By default, this method looks for a `-merge<Key>FromModel:` method on the
  38. // receiver, and invokes it if found. If not found, and `model` is not nil, the
  39. // value for the given key is taken from `model`.
  40. - (void)mergeValueForKey:(NSString *)key fromModel:(RQBaseModel *)model;
  41. // Merges the values of the given model object into the receiver, using
  42. // -mergeValueForKey:fromModel: for each key in +propertyKeys.
  43. //
  44. // `model` must be an instance of the receiver's class or a subclass thereof.
  45. - (void)mergeValuesForKeysFromModel:(RQBaseModel *)model;
  46. @end
  47. // Implements validation logic for RQBaseModel.
  48. @interface RQBaseModel (Validation)
  49. // Validates the model.
  50. //
  51. // The default implementation simply invokes -validateValue:forKey:error: with
  52. // all +propertyKeys and their current value. If -validateValue:forKey:error:
  53. // returns a new value, the property is set to that new value.
  54. //
  55. // error - If not NULL, this may be set to any error that occurs during
  56. // validation
  57. //
  58. // Returns YES if the model is valid, or NO if the validation failed.
  59. - (BOOL)validate:(NSError **)error;
  60. @end