diff --git a/Source/Constraint.swift b/Source/Constraint.swift index 6123ac5..de19995 100644 --- a/Source/Constraint.swift +++ b/Source/Constraint.swift @@ -231,9 +231,12 @@ public class Constraint { } internal func activateIfNeeded(updatingExisting: Bool = false) { - let view = self.from.view! + guard let view = self.from.view else { + print("WARNING: SnapKit failed to get from view from constraint. Activate will be a no-op.") + return + } let layoutConstraints = self.layoutConstraints - let existingLayoutConstraints = view.snp.layoutConstraints + let existingLayoutConstraints = view.snp.constraints.map({ $0.layoutConstraints }).reduce([]) { $0 + $1 } if updatingExisting { for layoutConstraint in layoutConstraints { @@ -247,14 +250,17 @@ public class Constraint { } } else { NSLayoutConstraint.activate(layoutConstraints) - view.snp.add(layoutConstraints: layoutConstraints) + view.snp.add(constraints: [self]) } } internal func deactivateIfNeeded() { - let view = self.from.view! + guard let view = self.from.view else { + print("WARNING: SnapKit failed to get from view from constraint. Deactivate will be a no-op.") + return + } let layoutConstraints = self.layoutConstraints NSLayoutConstraint.deactivate(layoutConstraints) - view.snp.remove(layoutConstraints: layoutConstraints) + view.snp.remove(constraints: [self]) } } diff --git a/Source/ConstraintMaker.swift b/Source/ConstraintMaker.swift index 8b10fcd..73bc3cd 100644 --- a/Source/ConstraintMaker.swift +++ b/Source/ConstraintMaker.swift @@ -180,7 +180,7 @@ public class ConstraintMaker { } internal static func updateConstraints(view: ConstraintView, closure: (_ make: ConstraintMaker) -> Void) { - guard view.snp.layoutConstraints.count > 0 else { + guard view.snp.constraints.count > 0 else { self.makeConstraints(view: view, closure: closure) return } @@ -197,7 +197,7 @@ public class ConstraintMaker { } internal static func removeConstraints(view: ConstraintView) { - let constraints = view.snp.layoutConstraints.map { $0.constraint! } + let constraints = view.snp.constraints for constraint in constraints { constraint.deactivateIfNeeded() } diff --git a/Source/ConstraintViewDSL.swift b/Source/ConstraintViewDSL.swift index 55bb845..541d18b 100644 --- a/Source/ConstraintViewDSL.swift +++ b/Source/ConstraintViewDSL.swift @@ -100,36 +100,36 @@ public struct ConstraintViewDSL: ConstraintAttributesDSL { } - internal var layoutConstraints: [LayoutConstraint] { - return self.layoutConstraintsHashTable.allObjects + internal var constraints: [Constraint] { + return self.constraintsHashTable.allObjects } - internal func add(layoutConstraints: [LayoutConstraint]) { - let hashTable = self.layoutConstraintsHashTable - for layoutConstraint in layoutConstraints { - hashTable.add(layoutConstraint) + internal func add(constraints: [Constraint]) { + let hashTable = self.constraintsHashTable + for constraint in constraints { + hashTable.add(constraint) } } - internal func remove(layoutConstraints: [LayoutConstraint]) { - let hashTable = self.layoutConstraintsHashTable - for layoutConstraint in layoutConstraints { - hashTable.remove(layoutConstraint) + internal func remove(constraints: [Constraint]) { + let hashTable = self.constraintsHashTable + for constraint in constraints { + hashTable.remove(constraint) } } - private var layoutConstraintsHashTable: NSHashTable { - let layoutConstraints: NSHashTable + private var constraintsHashTable: NSHashTable { + let constraints: NSHashTable - if let existing = objc_getAssociatedObject(self.view, &layoutConstraintsKey) as? NSHashTable { - layoutConstraints = existing + if let existing = objc_getAssociatedObject(self.view, &constraintsKey) as? NSHashTable { + constraints = existing } else { - layoutConstraints = NSHashTable.weakObjects() - objc_setAssociatedObject(self.view, &layoutConstraintsKey, layoutConstraints, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) + constraints = NSHashTable() + objc_setAssociatedObject(self.view, &constraintsKey, constraints, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) } - return layoutConstraints + return constraints } } -private var layoutConstraintsKey: UInt8 = 0 +private var constraintsKey: UInt8 = 0 diff --git a/Source/LayoutConstraint.swift b/Source/LayoutConstraint.swift index 3dbe3ab..27c4105 100644 --- a/Source/LayoutConstraint.swift +++ b/Source/LayoutConstraint.swift @@ -39,7 +39,7 @@ public class LayoutConstraint: NSLayoutConstraint { } } - internal var constraint: Constraint! = nil + internal weak var constraint: Constraint? = nil }