diff --git a/Snap.xcodeproj/project.pbxproj b/Snap.xcodeproj/project.pbxproj index d6151c3..70f4e6f 100644 --- a/Snap.xcodeproj/project.pbxproj +++ b/Snap.xcodeproj/project.pbxproj @@ -7,6 +7,8 @@ objects = { /* Begin PBXBuildFile section */ + EEAED5481A8F56A500777EF9 /* Snap.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = EEBCC9D819CC627D0083B827 /* Snap.framework */; }; + EEAED5491A8F56BF00777EF9 /* SnapTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = EE91728C19CB304E007888CF /* SnapTests.swift */; }; EEBCC9F019CC64F80083B827 /* EdgeInsets.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEBCC9EF19CC64F70083B827 /* EdgeInsets.swift */; }; EEBCC9F219CC65050083B827 /* View+Snap.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEBCC9F119CC65040083B827 /* View+Snap.swift */; }; EEBCC9F419CC65110083B827 /* ConstraintAttributes.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEBCC9F319CC65110083B827 /* ConstraintAttributes.swift */; }; @@ -46,6 +48,7 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( + EEAED5481A8F56A500777EF9 /* Snap.framework in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -233,6 +236,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + EEAED5491A8F56BF00777EF9 /* SnapTests.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Snap/ConstraintMaker.swift b/Snap/ConstraintMaker.swift index 2449ed9..69aaead 100644 --- a/Snap/ConstraintMaker.swift +++ b/Snap/ConstraintMaker.swift @@ -61,7 +61,7 @@ public class ConstraintMaker { return constraint } - internal class func makeConstraints(view: View, block: (make: ConstraintMaker) -> ()) { + internal class func makeConstraints(view: View, block: (make: ConstraintMaker) -> Void) { #if os(iOS) view.setTranslatesAutoresizingMaskIntoConstraints(false) #else @@ -70,7 +70,7 @@ public class ConstraintMaker { let maker = ConstraintMaker(view: view) block(make: maker) - var layoutConstraints = view.snp_installedLayoutConstraints + var layoutConstraints = Array(view.snp_installedLayoutConstraints) for constraint in maker.constraints { layoutConstraints += constraint.install() } @@ -78,7 +78,7 @@ public class ConstraintMaker { view.snp_installedLayoutConstraints = layoutConstraints } - internal class func remakeConstraints(view: View, block: (make: ConstraintMaker) -> ()) { + internal class func remakeConstraints(view: View, block: (make: ConstraintMaker) -> Void) { #if os(iOS) view.setTranslatesAutoresizingMaskIntoConstraints(false) #else @@ -87,7 +87,7 @@ public class ConstraintMaker { let maker = ConstraintMaker(view: view) block(make: maker) - var layoutConstraints: Array = view.snp_installedLayoutConstraints + var layoutConstraints = Array(view.snp_installedLayoutConstraints) for existingLayoutConstraint in layoutConstraints { existingLayoutConstraint.constraint?.uninstall() } @@ -100,7 +100,7 @@ public class ConstraintMaker { view.snp_installedLayoutConstraints = layoutConstraints } - internal class func updateConstraints(view: View, block: (make: ConstraintMaker) -> ()) { + internal class func updateConstraints(view: View, block: (make: ConstraintMaker) -> Void) { #if os(iOS) view.setTranslatesAutoresizingMaskIntoConstraints(false) #else @@ -109,7 +109,7 @@ public class ConstraintMaker { let maker = ConstraintMaker(view: view) block(make: maker) - var layoutConstraints = view.snp_installedLayoutConstraints + var layoutConstraints = Array(view.snp_installedLayoutConstraints) for constraint in maker.constraints { layoutConstraints += constraint.installOnView(updateExisting: true) } @@ -118,7 +118,8 @@ public class ConstraintMaker { } internal class func removeConstraints(view: View) { - for existingLayoutConstraint in view.snp_installedLayoutConstraints { + let eixsitingLayoutConstraints = Array(view.snp_installedLayoutConstraints) + for existingLayoutConstraint in eixsitingLayoutConstraints { existingLayoutConstraint.constraint?.uninstall() } diff --git a/Snap/View+Snap.swift b/Snap/View+Snap.swift index ebb0c19..7198c0f 100644 --- a/Snap/View+Snap.swift +++ b/Snap/View+Snap.swift @@ -68,15 +68,15 @@ public extension View { public var snp_centerWithinMargins: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.CenterWithinMargins) } #endif - public func snp_makeConstraints(block: (make: ConstraintMaker) -> ()) { + public func snp_makeConstraints(block: (make: ConstraintMaker) -> Void) { ConstraintMaker.makeConstraints(self, block: block) } - public func snp_updateConstraints(block: (make: ConstraintMaker) -> ()) { + public func snp_updateConstraints(block: (make: ConstraintMaker) -> Void) { ConstraintMaker.updateConstraints(self, block: block) } - public func snp_remakeConstraints(block: (make: ConstraintMaker) -> ()) { + public func snp_remakeConstraints(block: (make: ConstraintMaker) -> Void) { ConstraintMaker.remakeConstraints(self, block: block) } @@ -88,11 +88,10 @@ public extension View { internal var snp_installedLayoutConstraints: Array { get { - var constraints = objc_getAssociatedObject(self, &installedLayoutConstraintsKey) as? Array - if constraints != nil { - return constraints! + if let constraints = objc_getAssociatedObject(self, &installedLayoutConstraintsKey) as? Array { + return constraints } - return [] + return Array() } set { objc_setAssociatedObject(self, &installedLayoutConstraintsKey, newValue, UInt(OBJC_ASSOCIATION_RETAIN_NONATOMIC)) diff --git a/SnapTests/SnapTests.swift b/SnapTests/SnapTests.swift index 82665ad..bebb1b7 100644 --- a/SnapTests/SnapTests.swift +++ b/SnapTests/SnapTests.swift @@ -8,9 +8,12 @@ import UIKit import XCTest +import Snap class SnapTests: XCTestCase { + let container = UIView() + override func setUp() { super.setUp() // Put setup code here. This method is called before the invocation of each test method in the class. @@ -21,16 +24,73 @@ class SnapTests: XCTestCase { super.tearDown() } - func testExample() { - // This is an example of a functional test case. - XCTAssert(true, "Pass") + func testMakeConstraints() { + let v1 = UIView() + let v2 = UIView() + self.container.addSubview(v1) + self.container.addSubview(v2) + + v1.snp_makeConstraints { (make) -> Void in + make.top.equalTo(v2.snp_top).offset(50) + make.left.equalTo(v2.snp_top).offset(50) + return + } + v2.snp_makeConstraints { (make) -> Void in + make.edges.equalTo(v1) + return + } + } - func testPerformanceExample() { - // This is an example of a performance test case. - self.measureBlock() { - // Put the code you want to measure the time of here. + func testUpdateConstraints() { + let v1 = UIView() + let v2 = UIView() + self.container.addSubview(v1) + self.container.addSubview(v2) + + v1.snp_makeConstraints { (make) -> Void in + make.top.equalTo(v2.snp_top).offset(50) + make.left.equalTo(v2.snp_top).offset(50) + return } + v1.snp_updateConstraints { (make) -> Void in + make.top.equalTo(v2.snp_top).offset(15) + return + } + + } + + func testRemakeConstraints() { + let v1 = UIView() + let v2 = UIView() + self.container.addSubview(v1) + self.container.addSubview(v2) + + v1.snp_makeConstraints { (make) -> Void in + make.top.equalTo(v2.snp_top).offset(50) + make.left.equalTo(v2.snp_top).offset(50) + return + } + v1.snp_remakeConstraints { (make) -> Void in + make.edges.equalTo(v2) + return + } + + } + + func testRemoveConstraints() { + let v1 = UIView() + let v2 = UIView() + self.container.addSubview(v1) + self.container.addSubview(v2) + + v1.snp_makeConstraints { (make) -> Void in + make.top.equalTo(v2.snp_top).offset(50) + make.left.equalTo(v2.snp_top).offset(50) + return + } + v1.snp_removeConstraints() + } }