LookinAutoLayoutConstraint.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #ifdef SHOULD_COMPILE_LOOKIN_SERVER
  2. //
  3. // LookinAutoLayoutConstraint.m
  4. // Lookin
  5. //
  6. // Created by Li Kai on 2019/9/28.
  7. // https://lookin.work
  8. //
  9. #import "LookinAutoLayoutConstraint.h"
  10. #import "LookinObject.h"
  11. @implementation LookinAutoLayoutConstraint
  12. #if TARGET_OS_IPHONE
  13. + (instancetype)instanceFromNSConstraint:(NSLayoutConstraint *)constraint isEffective:(BOOL)isEffective firstItemType:(LookinConstraintItemType)firstItemType secondItemType:(LookinConstraintItemType)secondItemType {
  14. LookinAutoLayoutConstraint *instance = [LookinAutoLayoutConstraint new];
  15. instance.effective = isEffective;
  16. instance.active = constraint.active;
  17. instance.shouldBeArchived = constraint.shouldBeArchived;
  18. instance.firstItem = [LookinObject instanceWithObject:constraint.firstItem];
  19. instance.firstItemType = firstItemType;
  20. instance.firstAttribute = constraint.firstAttribute;
  21. instance.relation = constraint.relation;
  22. instance.secondItem = [LookinObject instanceWithObject:constraint.secondItem];
  23. instance.secondItemType = secondItemType;
  24. instance.secondAttribute = constraint.secondAttribute;
  25. instance.multiplier = constraint.multiplier;
  26. instance.constant = constraint.constant;
  27. instance.priority = constraint.priority;
  28. instance.identifier = constraint.identifier;
  29. return instance;
  30. }
  31. - (void)setFirstAttribute:(NSInteger)firstAttribute {
  32. _firstAttribute = firstAttribute;
  33. [self _assertUnknownAttribute:firstAttribute];
  34. }
  35. - (void)setSecondAttribute:(NSInteger)secondAttribute {
  36. _secondAttribute = secondAttribute;
  37. [self _assertUnknownAttribute:secondAttribute];
  38. }
  39. - (void)_assertUnknownAttribute:(NSInteger)attribute {
  40. // 以下几个 assert 用来帮助发现那些系统私有的定义,正式发布时应该去掉这几个 assert
  41. if (attribute > 20 && attribute < 32) {
  42. NSAssert(NO, nil);
  43. }
  44. if (attribute > 37) {
  45. NSAssert(NO, nil);
  46. }
  47. }
  48. #endif
  49. #pragma mark - <NSSecureCoding>
  50. + (BOOL)supportsSecureCoding {
  51. return YES;
  52. }
  53. - (void)encodeWithCoder:(NSCoder *)aCoder {
  54. [aCoder encodeBool:self.effective forKey:@"effective"];
  55. [aCoder encodeBool:self.active forKey:@"active"];
  56. [aCoder encodeBool:self.shouldBeArchived forKey:@"shouldBeArchived"];
  57. [aCoder encodeObject:self.firstItem forKey:@"firstItem"];
  58. [aCoder encodeInteger:self.firstItemType forKey:@"firstItemType"];
  59. [aCoder encodeInteger:self.firstAttribute forKey:@"firstAttribute"];
  60. [aCoder encodeInteger:self.relation forKey:@"relation"];
  61. [aCoder encodeObject:self.secondItem forKey:@"secondItem"];
  62. [aCoder encodeInteger:self.secondItemType forKey:@"secondItemType"];
  63. [aCoder encodeInteger:self.secondAttribute forKey:@"secondAttribute"];
  64. [aCoder encodeDouble:self.multiplier forKey:@"multiplier"];
  65. [aCoder encodeDouble:self.constant forKey:@"constant"];
  66. [aCoder encodeDouble:self.priority forKey:@"priority"];
  67. [aCoder encodeObject:self.identifier forKey:@"identifier"];
  68. }
  69. - (instancetype)initWithCoder:(NSCoder *)aDecoder {
  70. if (self = [super init]) {
  71. self.effective = [aDecoder decodeBoolForKey:@"effective"];
  72. self.active = [aDecoder decodeBoolForKey:@"active"];
  73. self.shouldBeArchived = [aDecoder decodeBoolForKey:@"shouldBeArchived"];
  74. self.firstItem = [aDecoder decodeObjectForKey:@"firstItem"];
  75. self.firstItemType = [aDecoder decodeIntegerForKey:@"firstItemType"];
  76. self.firstAttribute = [aDecoder decodeIntegerForKey:@"firstAttribute"];
  77. self.relation = [aDecoder decodeIntegerForKey:@"relation"];
  78. self.secondItem = [aDecoder decodeObjectForKey:@"secondItem"];
  79. self.secondItemType = [aDecoder decodeIntegerForKey:@"secondItemType"];
  80. self.secondAttribute = [aDecoder decodeIntegerForKey:@"secondAttribute"];
  81. self.multiplier = [aDecoder decodeDoubleForKey:@"multiplier"];
  82. self.constant = [aDecoder decodeDoubleForKey:@"constant"];
  83. self.priority = [aDecoder decodeDoubleForKey:@"priority"];
  84. self.identifier = [aDecoder decodeObjectForKey:@"identifier"];
  85. }
  86. return self;
  87. }
  88. @end
  89. #endif /* SHOULD_COMPILE_LOOKIN_SERVER */