NSObject+MJCoding.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. //
  2. // NSObject+MJCoding.h
  3. // MJExtension
  4. //
  5. // Created by mj on 14-1-15.
  6. // Copyright (c) 2014年 小码哥. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. #import "MJExtensionConst.h"
  10. /**
  11. * Codeing协议
  12. */
  13. @protocol MJCoding <NSObject>
  14. @optional
  15. /**
  16. * 这个数组中的属性名才会进行归档
  17. */
  18. + (NSArray *)mj_allowedCodingPropertyNames;
  19. /**
  20. * 这个数组中的属性名将会被忽略:不进行归档
  21. */
  22. + (NSArray *)mj_ignoredCodingPropertyNames;
  23. @end
  24. @interface NSObject (MJCoding) <MJCoding>
  25. /**
  26. * 解码(从文件中解析对象)
  27. */
  28. - (void)mj_decode:(NSCoder *)decoder;
  29. /**
  30. * 编码(将对象写入文件中)
  31. */
  32. - (void)mj_encode:(NSCoder *)encoder;
  33. @end
  34. /**
  35. 归档的实现
  36. */
  37. #define MJCodingImplementation \
  38. - (id)initWithCoder:(NSCoder *)decoder \
  39. { \
  40. if (self = [super init]) { \
  41. [self mj_decode:decoder]; \
  42. } \
  43. return self; \
  44. } \
  45. \
  46. - (void)encodeWithCoder:(NSCoder *)encoder \
  47. { \
  48. [self mj_encode:encoder]; \
  49. }\
  50. #define MJExtensionCodingImplementation MJCodingImplementation
  51. #define MJSecureCodingImplementation(CLASS, FLAG) \
  52. @interface CLASS (MJSecureCoding) <NSSecureCoding> \
  53. @end \
  54. @implementation CLASS (MJSecureCoding) \
  55. MJCodingImplementation \
  56. + (BOOL)supportsSecureCoding { \
  57. return FLAG; \
  58. } \
  59. @end \